Routes caching in L5

Laravel provides an easy way to cache your routes registration with the framework (not the response of the route) by running route:cache artisan command. If you get the following error:

  [LogicException]
  Unable to prepare route [/] for serialization. Uses Closure.

You can not cache routes that use Closure, as the docs specify:

If your application is exclusively using controller based routes, you may take advantageof Laravel's route cache.
Advertisements

Where do I put events? or bindings?

Laravel offers tons of features but no strict conventions, this is something I like a lot but can be discouraging for beginners because they don’t really know where to put their code (besides the mvc paradigm)

Events and Binds need to be injected in Laravel request cycle before routing, a good way of structuring them is to create 2 files in app/ :  binds.php and events.php which you include in app/start/global.php, at the end of the file:

require app_path().'/binds.php';

require app_path().'/events.php';

There really isn’t the “correct way”, it depends on you needs and your style. The example above is just one way of doing it.

How to user MySQL functions with eloquent and fluent

Both Eloquent ORM and Fluent query builder are wonderful tools, but, sometimes you want more, you may for example want to concatenate 2 fields (for presentation purposes Eloquent provides Getter & Setter Methods, but that’s another story for another time).

This:


 return self::get( array( 'id', 'CONCAT(first_name, " ", last_name)' ) );

Won’t work, to insert MySQL functions you have to use DB::raw() method:


 return self::get( array( 'id', DB::raw('CONCAT(first_name, " ", last_name)') ) );

How to organize base controllers

Situation: we have an application with multiple modules, each one needs a base controller that will handle common stuff for them, I prefer having a folder called “base_controllers” in application folder to keep it organized.

By default, Laravel’s base controller is in controllers folder(base.php) and it’s mapped in start.php with Autoloader::map().

To implement my way follow these 3 simple steps:

1. Delete base.php from controllers(if you don’t need it anymore)

2. Create base_controllers folder in /application/base_controllers and create the base controllers you need e.g. Base1.php and Base2.php:


Base1_Controller extends Controller {}
Base2_Controller extends Controller {}

3. To make it work(controllers to extend them) we have to ways:

3.1 In start.php with Autoloader::map() each one individually:


Autoloader::map(array(
     'Base1_Controller' => path('app').'base_controllers/Base1.php',
     'Base2_Controller' => path('app').'base_controllers/Base2.php',
));

3.2 In start.php with Autoloader::directories():


Autoloader::directories(array(
       path('app').'models',
       path('app').'libraries',
       path('app').'base_controllers',
));

Note: Laravel’s autoloader will search the directories using the PSR-0 naming convention.

How to use mysql_insert_id in Laravel

At this moment the docs are very vague about this, searching “mysql_insert_id” won’t return anything but if you look through the docs you’ll find multiple ways to get that id:

With PDO

$id = DB::connection('mysql')->pdo->lastInsertId();

With insert_get_id() method that works with Fluent or Eloquent objects(will work only if the name of the auto-incrementing column is “id”):

$id = DB::table('users')->insert_get_id(array('name' => 'John Doe'));

With Eloquent’s create() method, which returns the model instance for the newly inserted record, or false if the insert failed.

$user = User::create(array('name' => 'John Doe'));

$user->id;