2. 新增自定義的全域函式(helpers function)

Laravel 定義了很多常用的全域變數,但如果我們想要自己定義的話,就須要靠這個來執行了,譬如每頁都會有專有的<h1>標題,若要每頁都寫的話,未免太麻煩!不如開一個views來專門顯示就好,所以需要定義一個Function來判斷目前頁面的別名,然後自動取得對應的標題名稱即可。

開啟 config/app.php
     /*  
      * Application Service Providers...  
      */  
     App\Providers\AppServiceProvider::class,  
     App\Providers\EventServiceProvider::class,  
     App\Providers\RouteServiceProvider::class,  
     App\Providers\HelperServiceProvider::class,  

新增 app/Providers/HelperServiceProvider.php
 <?php  
 namespace App\Providers;  
 use Illuminate\Support\ServiceProvider;  
 class HelperServiceProvider extends ServiceProvider  
 {  
     /**  
      * Bootstrap the application services.  
      *  
      * @return void  
      */  
     public function boot()  
     {  
     }  
     /**  
      * Register the application services.  
      *  
      * @return void  
      */  
     public function register()  
     {  
     foreach (glob(app_path().'/Helpers/*.php') as $filename)  
     {  
       require_once($filename);  
     }  
     }  
 }  

新增 app/Helpers/AdminHelper.php
 if ( ! function_exists('breadcrumbs'))  
 {  
   /**  
    * Return breadcrumbs for each resource methods  
    *  
    * @return string  
    */  
   function breadcrumbs()  
   {  
     $route = Route::currentRouteName();  
     // get after last dot  
     $index = substr($route, 0, strrpos($route, '.') + 1) . 'index';  
     $breadcrumbs = '<div class="page-bar">';  
     $breadcrumbs .= '<ul class="page-breadcrumb">';  
     $breadcrumbs .= '<li><i class="fa fa-home"></i> <a href="'.route('admin.root').'"> '.trans('admin.menu.home').'</a></li>';  
     // if not admin root  
     if(strpos($route, 'root') === false)  
     {  
       $breadcrumbs .= strpos($route, 'index') !== false ? '<li class="active">' : '<li>';  
       $parent_text  = strpos($route, 'index') !== false ? trans($route) : trans($index);  
       $breadcrumbs .= strpos($route, 'index') !== false ? $parent_text : '<i class="fa fa-angle-right"></i> <a href="'.route($index).'">'.$parent_text.'</a>';  
       $breadcrumbs .= '</li>';  
       if(strpos($route, 'index') === false)  
       {  
         $breadcrumbs .= '<li class="active"><i class="fa fa-angle-right"></i>'.trans($route).'</li>';  
       }  
     }  
     $breadcrumbs .= '</ul></div>';  
     return $breadcrumbs;  
   }  
 }  
 if ( ! function_exists('header_title'))  
 {  
   /**  
    * Return the header title for each page  
    *  
    * @return string  
    */  
   function header_title()  
   {  
     $route = Route::currentRouteName();  
     $title = '<h1 class="page-title">';  
     $title .= trans(Route::getCurrentRoute()->getName());  
     if( strpos($route, 'index') !== false )  
     {  
       $new = substr($route, 0, strrpos($route, '.') + 1) . 'create';  
       if(Route::has($new))  
       {  
         $title .= '<small>';  
         $title .= '<a href="'.route($new).'" title="'.trans($new).'">';  
         $title .= '<i class="fa fa-plus"></i>';  
         $title .= '</a>';  
         $title .= '</small>';  
       }  
     }  
     $title .= '</h1>';  
     return $title;  
   }  
 }  

在適當的viess插入:(ex: header.blade.php)
   {!! header_title() !!}  
   {!! breadcrumbs() !!}  

就可以看到目前頁面的名稱(by route別名): header_title(),以及頁面層級 breadcrumbs()

留言

這個網誌中的熱門文章

3. Sentinel 2.0 用戶認證應用

0. env 基礎設置