Laravel 自動分發路由處理及全局處理輔助函數

 首先我們都知道laravel既可以分發web端路由,也可以分發api端路由  但是卻如何將api的控制器與web端控制器相互分離,

那麼本次就跟大家分享下 如何來將web/api業務處理分開

1 設置 app->Providers:RouteServiceProvider.php

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your web controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $webNameSpace = 'App\Http\Controllers';
    
    /**
     * This namespace is applied to your api controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $apiNameSpace = 'App\Http\Apis';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware( 'web' )
             ->namespace( $this->webNameSpace )
             ->group( base_path( 'routes/web.php' ) );
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace( $this->apiNameSpace )
             ->group( base_path( 'routes/api.php' ) );
    }

那麼我們看到 map方法下調取兩種匹配路由方式 api web  二者主要根據不同的規則分發加載不同的路有文件進行url分發

2 創建對應文件 app->Apis

我們可將web控制器中基類 App\Http\Controllers\Controller.php複製至該文件下

接下來就可以隨意的開始你的表演了

            ---記得在url訪問的時候默認加上 api 前綴 http://domain.baidu.com/api/users/list

 

 

Laravel 如何加全局輔助函數:

   1 找到composer.json文件 autoload 屬性下添加

"files": [  
    "app/Helpers/helpers.php"  
]  

2 創建對應文件:helpers.php

<?php


if (! function_exists('storage_day_path')) {
    /**
     * Get the path to the storage folder.
     *
     * @param  string  $path
     * @return string
     */
    function storage_day_path( $file = '')
    {
        $today = date( 'Y-m-d', time() );
        $todayPath = app('path.storage') . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $today;
        if( !is_dir( $todayPath ) || !file_exists( $todayPath ) )
        {
            mkdir( $todayPath, 0777 );
        }
        return $todayPath . ( $file? DIRECTORY_SEPARATOR.$file: $file );
    }
}


if (! function_exists( 'format_date' ) ) {
    /**
     * Get the path to the storage folder.
     *
     * @param  string  $path
     * @return string
     */
    function format_date( $datetime = '' )
    {
        $formatTimeStamp = strtotime( $datetime );
        if( is_numeric( $datetime ) )
        {
            $formatTimeStamp = $datetime;
        }
        $currYear   = date( 'Y', time() );
        $formatYear = date( 'Y', $formatTimeStamp );
        if( $currYear == $formatYear )
        {
            return date( 'm-d H:i', $formatTimeStamp );
        }
        
        return date( 'Y-m-d', $formatTimeStamp );
    }
}

**** 若沒生效的話 可以嘗試 composer  dump-autoload 

那麼這種子以後,我們的控制器/模型/視圖 等等都可以使用該函數進行處理。 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章