【问题标题】:laravel 5.2 pass variable in all controllerslaravel 5.2 在所有控制器中传递变量
【发布时间】:2020-05-01 22:45:41
【问题描述】:

有人可以帮忙吗,我想让我的变量在所有控制器中都是全局的,而不是每次都创建实例.. settings = new Setting(); settings 只有一行。

控制器类扩展 BaseController

protected $settings;

    public function __construct() 
    {
        $this->settings = Setting::all();
        View::share('settings', $this->settings);
    }
}

在我使用的另一个控制器中:

$this->settings->email;

但它不起作用,请我搜索并看到这个方法不好,有人可以给我一个好的方法,laravel 5.2。

非常感谢..!!

【问题讨论】:

    标签: laravel laravel-5 controller


    【解决方案1】:

    为视图使用提供者。对控制器使用扩展控制器。确保您的另一个控制器扩展了包含全局设置值的控制器。确保您的另一个控制器没有 __construct 或具有 parent::__construct 的构造。

    例如你可以使用2种方式:

    class Controller extends BaseController{
        protected $settings;
        public function __construct()
        {
            $this->settings = Setting::all();
        }
    }
    

    然后

    class AnotherController extends Controller{
    
        public function __construct(){
            parent::__construct();
         //blahblah
        }
        public function anyMethod(){
            $asd = $this->settings;
        }
    }
    

    或

    class AnotherController extends Controller{
    
        public function anyMethod(){
            $asd = $this->settings;
        }
    }
    

    如果要使用视图文件,请在 Providers 中使用 AppServiceProvider.php

    public function boot ()
    {
        $settings = Setting::all();
        view ()->composer ( '*', function ( $view ) use ( $settings ) {
                    $view->with ( 'settings', $settings );
                } );
    }
    

    【讨论】:

    • 这对您有帮助吗?
    【解决方案2】:

    只需将您的设置变量添加到父控制器。

    App/Http/Controllers/Controller.php

    namespace App\Http\Controllers;
    
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Illuminate\Foundation\Validation\ValidatesRequests;
    use Illuminate\Routing\Controller as BaseController;
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
        public $settings;
    
        public function settings(){
           $this->settings = \App\Setting::all();
           return $this->settings;
        }
    }
    

    您的应用程序控制器扩展了控制器,您只需要添加一个构造函数或调用 $this->settings() 它将您的结果加载到控制器中的 $this->settings 中。

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class MyController extends Controller{
    
        public function __construct(){
            $this->settings();
        }
    
        public function someMethod(){
            // this is now available with results already loaded.
            $this->settings // option 1
            $this->settings() // this is the same as option 1 but runs the query when called and populates this settings property and returns the results.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-03
      • 1970-01-01
      • 2016-11-25
      • 2016-12-17
      • 1970-01-01
      • 2016-05-16
      • 2015-01-06
      • 2016-08-29
      • 2017-01-30
      相关资源
      最近更新 更多