资讯专栏INFORMATION COLUMN

Laravel中你为什么可以直接在 web.php 中 直接使用 Route ? 服务提供者的介绍

desdik / 938人阅读

摘要:这篇文章来自一个社区问题的思考中为什么可以直接使用原理很简单首先你注意一下里面

这篇文章来自一个 sf 社区问题的思考

laravel web.php 中 Route 为什么可以直接使用


原理很简单

1 . 首先, 你注意一下 /config/app.php 里面

/*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don"t hinder performance.
    |
    */
    "aliases" => [
        "Route" => IlluminateSupportFacadesRoute::class,
    ];

2 . 因为有 Facades, 所以我们直接去看 IlluminateSupportFacadesRoute::class 这个类返回的内容

* @method static IlluminateRoutingRoute get(string $uri, Closure|array|string $action)

/**
 * Get the registered name of the component.
 *
 * @return string
 */
protected static function getFacadeAccessor()
{
    return "router";
}

3 . 那就简单了, 直接去找注册为 router 的组件

发现是在 Illuminate/Routing/RoutingServiceProvider.php

/**
 * Register the router instance.
 *
 * @return void
 */
protected function registerRouter()
{
    $this->app->singleton("router", function ($app) {
        return new Router($app["events"], $app);
    });
}

4 . new Router() 看到了没, 很显然就会返回 Illuminate/Routing/Router.php 实例; 是不是发现了

   /**
     * Register a new GET route with the router.
     *
     * @param  string  $uri
     * @param  Closure|array|string|null  $action
     * @return IlluminateRoutingRoute
     */
    public function get($uri, $action = null)
    {
        return $this->addRoute(["GET", "HEAD"], $uri, $action);
    }

(づ ̄3 ̄)づ╭❤~ 宣我 !! 么么
问答时间

1) . 我确认了 "router" 是在
Illuminate/Routing/RoutingServiceProvider.php 里面的 ,

但是为什么没有配置在 /config/app.phpproviders 里面呢

答案在这里

Illuminate/Foundation/Application.php

注意 base service providersconfigured providers


    /**
     * Register all of the base service providers.
     *
     * @return void
     */
    protected function registerBaseServiceProviders()
    {
        $this->register(new EventServiceProvider($this));

        $this->register(new LogServiceProvider($this));

        $this->register(new RoutingServiceProvider($this));
    }
    
    

   /**
     * Register all of the configured providers.
     *
     * @return void
     */
    public function registerConfiguredProviders()
    {
        (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath()))
                    ->load($this->config["app.providers"]);
    }

2) . 那我看到在 /config/app.php 注册了一个看起来像路由相关的 AppProvidersRouteServiceProvider::class, 它是干嘛用的呢?

答案在这里

首先 AppProvidersRouteServiceProvider 继承自 IlluminateFoundationSupportProvidersRouteServiceProvider; (并且调用了我们上面的 IlluminateSupportFacadesRoute, 可以使用 Route::* )

直接看看 IlluminateFoundationSupportProvidersRouteServiceProvider 你就会明白

    public function boot()
    {
        $this->setRootControllerNamespace();

        if ($this->app->routesAreCached()) {
            $this->loadCachedRoutes();
        } else {
            $this->loadRoutes();

            $this->app->booted(function () {
                $this->app["router"]->getRoutes()->refreshNameLookups();
                $this->app["router"]->getRoutes()->refreshActionLookups();
            });
        }
    }
    
    public function register()
    {
        // 没有在这里注册
    }

providers文档

boot 方法是在所有服务提供者都注册完成后调用的方法, 所以说 这是启动后 注册路由的 provider

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/23332.html

相关文章

  • 【日常填坑】之ajax请求laravelapi接口

    摘要:合适和够用是最完美的追求。比如从页面去请求的资源。它允许浏览器向跨源服务器,发出请求,从而克服了只能同源使用的限制。定义在中的路由都是无状态的,并且会应用中间件组。 关于作者 程序开发人员,不拘泥于语言与技术,目前主要从事PHP和前端开发,使用Laravel和VueJs,App端使用Apicloud混合式开发。合适和够用是最完美的追求。 个人网站:http://www.linganm...

    Arno 评论0 收藏0
  • 【日常填坑】之ajax请求laravelapi接口

    摘要:合适和够用是最完美的追求。比如从页面去请求的资源。它允许浏览器向跨源服务器,发出请求,从而克服了只能同源使用的限制。定义在中的路由都是无状态的,并且会应用中间件组。 关于作者 程序开发人员,不拘泥于语言与技术,目前主要从事PHP和前端开发,使用Laravel和VueJs,App端使用Apicloud混合式开发。合适和够用是最完美的追求。 个人网站:http://www.linganm...

    neu 评论0 收藏0
  • 【日常填坑】之ajax请求laravelapi接口

    摘要:合适和够用是最完美的追求。比如从页面去请求的资源。它允许浏览器向跨源服务器,发出请求,从而克服了只能同源使用的限制。定义在中的路由都是无状态的,并且会应用中间件组。 关于作者 程序开发人员,不拘泥于语言与技术,目前主要从事PHP和前端开发,使用Laravel和VueJs,App端使用Apicloud混合式开发。合适和够用是最完美的追求。 个人网站:http://www.linganm...

    fuyi501 评论0 收藏0
  • Repository模式下使用laravel

    摘要:仓库地址文档地址清晰的目录结构只负责定义模型如模型关联和等负责处理这个表相关的所有业务逻辑不只是注入相关的任何都可以注入代码定位迅速只负责处理简单的逻辑获取转发数据它应该是简洁干净的所有的验证类所有的模型用户相关的所有模型目录结构应与一致 laravel-repository 仓库地址Github Repository文档地址 清晰的目录结构 Models只负责定义模型(如:模型关联,...

    netScorpion 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<