Tuesday, July 23, 2013

Adding new classes or library to laravel 4

The ugly, lazy and awful way: At the end of bootstrap/start.php , add an include('tools.php') and place your function in that new file.

The clean way: Create a library. That way it'll be autoloaded ONLY when you actually use it.
  • Create a library folder inside your app folder
  • Edit start/global.php to add app_path().'/library' to the ClassLoader::addDirectories( array.
  • Edit composer.json to add "app/library" to the autoload array.
  • Run composer dump-autoload
  • Create your library file, create a class in it, and add static functions to it
  • Call your class and static functions from your views.
Credit goes to this guy

No comments:

Post a Comment