nikish ryumin
nikish ryumin

Reputation: 46

Laravel router namespace method

In Laravel documentation routing there is a namespace method.

Route::namespace

I tried to explore what does it really do but couldn't find it's definition in Laravel source codes. Where is it?

Upvotes: 0

Views: 1435

Answers (1)

Taha Paksu
Taha Paksu

Reputation: 15616

It's not related to code, just to group the routes. Like this:

The source is here: https://github.com/laravel/framework/blob/b73691ac7b309cd2c4fb29b32d3eed76fecca58b/src/Illuminate/Routing/RouteGroup.php#L40, it just adds the namespace at end of the current namespace.

You have a controller group like 'Products' for example,

App/
    Http/
        Controllers/
            Products/
                Stocks.php 
                Prices.php
                Sizes.php 

And you need to modify their namespaces like this to meet the PSR-4 requirements to enable autoloading of controllers:

namespace App\Http\Controllers\Products;            

class Stocks {
      function index(){

      }
}

Then if you want to access the methods of these controllers, you might want to group them with Route::namespace():

Route::namespace("Products")->group(function(){
    Route::get("stocks", "Stocks@index");
});

This will search for the Stocks class in the App\Http\Controllers\Products namespace instead of App\Http\Controllers namespace. and call the index method.

Note that you might run composer dumpautoload to let the framework rebuild the autoload.php with the PSR-4 namespaces to make these things effective.


Later Edit:

framework/src/Illuminate/Routing/Router.php defines the Route class, which redirects the Route::namespace method to RouterRegistrar class at this line:

/**
 * Dynamically handle calls into the router instance.
 *
 * @param  string  $method
 * @param  array  $parameters
 * @return mixed
 */
public function __call($method, $parameters)
{
    if (static::hasMacro($method)) {
        return $this->macroCall($method, $parameters);
    }
    if ($method == 'middleware') {
        return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
    }
    return (new RouteRegistrar($this))->attribute($method, $parameters[0]);
}

in the last line. And in that method,

/**
 * The attributes that can be set through this class.
 *
 * @var array
 */
protected $allowedAttributes = [
    'as', 'domain', 'middleware', 'name', 'namespace', 'prefix',
];

/**
 * Set the value for a given attribute.
 *
 * @param  string  $key
 * @param  mixed  $value
 * @return $this
 *
 * @throws \InvalidArgumentException
 */
public function attribute($key, $value)
{
    if (! in_array($key, $this->allowedAttributes)) {
        throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
    }
    $this->attributes[Arr::get($this->aliases, $key, $key)] = $value;
    return $this;
}

namespace attribute is being set, to use in ->group() method.

Upvotes: 2

Related Questions