php - Symfony2 re-usable snippet functions -


i using symfony2 lately , keep wondering when should create service , when should create class.

for example have little snippet function reusable piece of code. because of not want piece of code in controller. meaning want use anywhere in bundle.

it's simple, generates full url me based on slug/path. please note else example customstringgenerator() or ever.

the snippet:

public function generateurlfrompath(request $request, $path) {     return $request->getscheme() . '://' . $request->gethttphost() . $request->getbasepath() . $path; } 

now make sort of toolkit or urlhelper class , define service can inject request object, seems overdone me create service such simple method.

what suggest in case? guys when have created reusable functions?

thanks far.

for reusability, use:

  • services (when need inject other dependencies through constructor), don't have manually everytime.

  • helper controller, controllers extend.

  • classes public static functions.

  • traits (php >= 5.4), when need share same methods in few classes, overcome single class inheritance limitation.

for function there, make protected function inside helper controller, service ok too, such small piece of code. might end writing more code service later.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -