成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Laravel中你為什么可以直接在 web.php 中 直接使用 Route ? 服務(wù)提供者的介紹

desdik / 1092人閱讀

摘要:這篇文章來(lái)自一個(gè)社區(qū)問(wèn)題的思考中為什么可以直接使用原理很簡(jiǎn)單首先你注意一下里面

這篇文章來(lái)自一個(gè) sf 社區(qū)問(wèn)題的思考

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


原理很簡(jiǎn)單

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 . 因?yàn)橛?Facades, 所以我們直接去看 IlluminateSupportFacadesRoute::class 這個(gè)類返回的內(nèi)容

* @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 . 那就簡(jiǎn)單了, 直接去找注冊(cè)為 router 的組件

發(fā)現(xiàn)是在 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() 看到了沒(méi), 很顯然就會(huì)返回 Illuminate/Routing/Router.php 實(shí)例; 是不是發(fā)現(xiàn)了

   /**
     * 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 ̄)づ╭?~ 宣我 !! 么么
問(wèn)答時(shí)間

1) . 我確認(rèn)了 "router" 是在
Illuminate/Routing/RoutingServiceProvider.php 里面的 ,

但是為什么沒(méi)有配置在 /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 注冊(cè)了一個(gè)看起來(lái)像路由相關(guān)的 AppProvidersRouteServiceProvider::class, 它是干嘛用的呢?

答案在這里

首先 AppProvidersRouteServiceProvider 繼承自 IlluminateFoundationSupportProvidersRouteServiceProvider; (并且調(diào)用了我們上面的 IlluminateSupportFacadesRoute, 可以使用 Route::* )

直接看看 IlluminateFoundationSupportProvidersRouteServiceProvider 你就會(huì)明白

    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()
    {
        // 沒(méi)有在這里注冊(cè)
    }

providers文檔

boot 方法是在所有服務(wù)提供者都注冊(cè)完成后調(diào)用的方法, 所以說(shuō) 這是啟動(dòng)后 注冊(cè)路由的 provider

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/23332.html

相關(guān)文章

  • 【日常填坑】之a(chǎn)jax請(qǐng)求laravelapi接口

    摘要:合適和夠用是最完美的追求。比如從頁(yè)面去請(qǐng)求的資源。它允許瀏覽器向跨源服務(wù)器,發(fā)出請(qǐng)求,從而克服了只能同源使用的限制。定義在中的路由都是無(wú)狀態(tài)的,并且會(huì)應(yīng)用中間件組。 關(guān)于作者 程序開發(fā)人員,不拘泥于語(yǔ)言與技術(shù),目前主要從事PHP和前端開發(fā),使用Laravel和VueJs,App端使用Apicloud混合式開發(fā)。合適和夠用是最完美的追求。 個(gè)人網(wǎng)站:http://www.linganm...

    Arno 評(píng)論0 收藏0
  • 【日常填坑】之a(chǎn)jax請(qǐng)求laravelapi接口

    摘要:合適和夠用是最完美的追求。比如從頁(yè)面去請(qǐng)求的資源。它允許瀏覽器向跨源服務(wù)器,發(fā)出請(qǐng)求,從而克服了只能同源使用的限制。定義在中的路由都是無(wú)狀態(tài)的,并且會(huì)應(yīng)用中間件組。 關(guān)于作者 程序開發(fā)人員,不拘泥于語(yǔ)言與技術(shù),目前主要從事PHP和前端開發(fā),使用Laravel和VueJs,App端使用Apicloud混合式開發(fā)。合適和夠用是最完美的追求。 個(gè)人網(wǎng)站:http://www.linganm...

    neu 評(píng)論0 收藏0
  • 【日常填坑】之a(chǎn)jax請(qǐng)求laravelapi接口

    摘要:合適和夠用是最完美的追求。比如從頁(yè)面去請(qǐng)求的資源。它允許瀏覽器向跨源服務(wù)器,發(fā)出請(qǐng)求,從而克服了只能同源使用的限制。定義在中的路由都是無(wú)狀態(tài)的,并且會(huì)應(yīng)用中間件組。 關(guān)于作者 程序開發(fā)人員,不拘泥于語(yǔ)言與技術(shù),目前主要從事PHP和前端開發(fā),使用Laravel和VueJs,App端使用Apicloud混合式開發(fā)。合適和夠用是最完美的追求。 個(gè)人網(wǎng)站:http://www.linganm...

    fuyi501 評(píng)論0 收藏0
  • Repository模式下使用laravel

    摘要:倉(cāng)庫(kù)地址文檔地址清晰的目錄結(jié)構(gòu)只負(fù)責(zé)定義模型如模型關(guān)聯(lián)和等負(fù)責(zé)處理這個(gè)表相關(guān)的所有業(yè)務(wù)邏輯不只是注入相關(guān)的任何都可以注入代碼定位迅速只負(fù)責(zé)處理簡(jiǎn)單的邏輯獲取轉(zhuǎn)發(fā)數(shù)據(jù)它應(yīng)該是簡(jiǎn)潔干凈的所有的驗(yàn)證類所有的模型用戶相關(guān)的所有模型目錄結(jié)構(gòu)應(yīng)與一致 laravel-repository 倉(cāng)庫(kù)地址Github Repository文檔地址 清晰的目錄結(jié)構(gòu) Models只負(fù)責(zé)定義模型(如:模型關(guān)聯(lián),...

    netScorpion 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<