duanguan3863 2015-04-13 07:11
浏览 190
已采纳

Laravel 5无法将变量传递给所有视图

I have a problem with passing variable (data) to all Views. I created BaseController that extends default Laravel controller and "global" variables are defined there. When I extend other controller with BaseController i got error that variable is not defined. Does someone knows where's the problem?

Here is code:

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;

class BaseController extends Controller {
    public function __construct() {
        $obavijesti="Other data";
        $izbornici="Some data";
        View::share ( 'izbornici', $izbornici );
        View::share ( 'obavjesti', $obavjesti );
        }
   }

class AdminController extends BaseController {
     .
     .
     .
      echo '<pre>';var_dump($izbornici);echo '</pre>';//Error pop ups here

     .
     .
     .
}
  • 写回答

2条回答 默认 最新

  • duanmao1975 2015-04-13 07:32
    关注

    You are doing something wrong here. view::share() is used for sharing a piece of data across all views not controller.

    namespace App\Http\Controllers;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Sentry;
    use Illuminate\Http\Request;
    use View;
    
    //If you wish to get these variables in your other controllers you do this:
    
    class BaseController extends Controller {
    
        public $obavijesti="Other data";
        public $izbornici="Some data";
    
        public function __construct() {
           View::share ( 'izbornici', $this->izbornici );
           View::share ( 'obavjesti', $this->obavjesti );
        }  
    
    }
    
    class AdminController extends BaseController {
    
        //if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:
    
        public function __construct(){
           parent::__construct();
        }
    
        public function Index(){
          echo $this->obavijesti;
        }
    
    }
    

    You can also use a composer to share variables to views

    //1. Create a composer file at app\Composers\AdminComposer.php
    //NB: create "app\Composers" if does not exists
    
    //2. Inside AdminComposer.php add this.
    
    
    <?php namespace App\Composers;
    
    class AdminComposer
    {
        public function __construct()
        {
    
        }
    
        public function compose($view)
        {
            //Add your variables
            $view->with('izbornici',      'Other data')
                ->with('obavjesti',       'Some other data');
        }
    }
    
    //3. In you controller do this:
    
    <?php namespace App\Http\Controllers;
    //NB: I removed your BaseController because I believe the issue is coming from //there
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Sentry;
    use Illuminate\Http\Request;
    use View;
    
    class AdminController extends Controller{
    
    
        public function __construct(){
            //Lets use AdminComposer to share variables to adminpage.blade.php view
    
            View::composers([
                'App\Composers\AdminComposer'  => array('adminpage')
            ]);
    
        }
    
        public function Index(){
            return view('adminpage');
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器