Laravel路由器名称空间方法

Laravel路由器名称空间方法,laravel,Laravel,在Laravel文档中有一个名称空间方法 Route::namespace 我试图探索它到底做了什么,但在Laravel源代码中找不到它的定义。它在哪里?它与代码无关,只是对路线进行分组。像这样: 源代码在这里:,它只是将名称空间添加到当前名称空间的末尾 您有一个控制器组,例如“产品” App/ Http/ Controllers/ Products/ Stocks.php Pri

在Laravel文档中有一个名称空间方法

Route::namespace

我试图探索它到底做了什么,但在Laravel源代码中找不到它的定义。它在哪里?

它与代码无关,只是对路线进行分组。像这样:

源代码在这里:,它只是将名称空间添加到当前名称空间的末尾

您有一个控制器组,例如“产品”

App/
    Http/
        Controllers/
            Products/
                Stocks.php 
                Prices.php
                Sizes.php 
您需要像这样修改它们的名称空间,以满足PSR-4要求,从而启用控制器的自动加载:

namespace App\Http\Controllers\Products;            

class Stocks {
      function index(){

      }
}
然后,如果要访问这些控制器的方法,可能需要使用
Route::namespace()

这将搜索
App\Http\Controllers\Products
命名空间中的
Stocks
类,而不是
App\Http\Controllers
命名空间。并调用
index
方法

注意您可以运行
composer dumpautoload
,让框架使用PSR-4名称空间重新构建autoload.php,从而使这些事情变得有效


以后编辑:

framework/src/illusted/Routing/Router.php
定义了
Route
类,它将
Route::namespace
方法重定向到此行的
routerregistar
类:

/**
 * 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]);
}
在最后一行。用这种方法

/**
 * 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;
}

名称空间
属性被设置为在
->group()
方法中使用。

它与代码无关,只是对路由进行分组。像这样:

源代码在这里:,它只是将名称空间添加到当前名称空间的末尾

您有一个控制器组,例如“产品”

App/
    Http/
        Controllers/
            Products/
                Stocks.php 
                Prices.php
                Sizes.php 
您需要像这样修改它们的名称空间,以满足PSR-4要求,从而启用控制器的自动加载:

namespace App\Http\Controllers\Products;            

class Stocks {
      function index(){

      }
}
然后,如果要访问这些控制器的方法,可能需要使用
Route::namespace()

这将搜索
App\Http\Controllers\Products
命名空间中的
Stocks
类,而不是
App\Http\Controllers
命名空间。并调用
index
方法

注意您可以运行
composer dumpautoload
,让框架使用PSR-4名称空间重新构建autoload.php,从而使这些事情变得有效


以后编辑:

framework/src/illusted/Routing/Router.php
定义了
Route
类,它将
Route::namespace
方法重定向到此行的
routerregistar
类:

/**
 * 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]);
}
在最后一行。用这种方法

/**
 * 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;
}

名称空间
属性正在设置,以在
->group()
方法中使用。

这是一个名称空间,用于指定从何处加载路由控制器。这是一个名称空间,用于指定路由控制器的位置我想他关心的是,您找不到函数“namespace”,而是“formatNamespace”,但文档中没有引用“namespace”方法。有别名吗?重定向?机制是什么?文档错误吗?此行处理不存在的方法,这些方法注册为宏,而不是直接方法。我将查找并共享宏。我添加了设置和使用
名称空间
属性的路径。我猜他担心的是,您找不到函数“namespace”,而是“formatNamespace”,但文档中没有引用“namespace”方法。有别名吗?重定向?机制是什么?文档错误吗?此行处理不存在的方法,这些方法注册为宏,而不是直接方法。我将查找并共享宏。我添加了设置并使用
名称空间
属性的路径。