Laravel Route Jenkyness

If you start a new Laravel 5 project, you may notice that Netbeans doesn't know what certain items are. For example if you open the routes file at /app/Http/routes.php you will see the following:

Route::get('/', function () {  
    return view('welcome');
});

Now if you double click on Route and then press cntrl-b to go to source, it will fail. Moreover, if you type Route::, auto complete will not come up with any methods/functions or attributes because netbeans has no idea what Route is. Yet this code will still work, how can this be?

Working backwards, you will see that the RouteServiceProvider.php file includes the routes.php file directly inside a callback that it defines within the map function. It may take you a while to register that by reading it, so it's easier just to read the code:

public function map(Router $router)  
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
    });
}

It is in this file that you will see Router (not Route) as a typehint to the parameter the method takes. Netbeans knows what this object is because there is this line near the top of the file:

use Illuminate\Routing\Router;  

All the code written in routes.php is put inside an inline function that is passed to the Router object's "group" method, so as long as this Router function knows what Route is then the code will work. Go to source on the Router function and check it out.

Near the top of this class, you will see the following:

namespace Illuminate\Routing;  

Guess what? Route.php is a class within that namespace. This means that if you type Route::in here you will suddenly get its public methods and properties.

So in summary, all the code you write in routes.php, for all intents and purposes, "ends up" in this file within the group method (inside $callback) where it can be understood as shown below:

public function group(array $attributes, Closure $callback)  
{
    $this->updateGroupStack($attributes);

    // Once we have updated the group stack, we will execute the user Closure and
    // merge in the groups attributes when the route is created. After we have
    // run the callback, we will pop the attributes off of this group stack.
    call_user_func($callback, $this);

    array_pop($this->groupStack);
}

This feel very jenky to me, and I would have preferred it if the codebase had done one of these options instead (which you can use).

  • Use the full path in route calls on routes.php. For example:
Illuminate\Routing\Route::get('/', function () {  
    return view('welcome');
});
  • Add namespace Illuminate\Routing; to the top of routes.php file.

I'm sure there are more of these and I will provide suggestions as and when I find them.

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites. More info.