Laravel 5无法将变量传递给所有视图 [英] Laravel 5 fails to pass variable to all views

查看:89
本文介绍了Laravel 5无法将变量传递给所有视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将变量(数据)传递给所有视图时遇到问题.我创建了BaseController来扩展默认的Laravel控制器,并在那里定义了全局"变量.当我使用BaseController扩展其他控制器时,出现错误,未定义变量.有人知道哪里出问题了吗?

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?

这是代码:

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

     .
     .
     .
}

推荐答案

您在这里做错了什么. view :: share()用于在所有视图而非控制器之间共享一条数据.

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;
    }

}

您还可以使用编辑器将变量共享给视图

//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');
    }

}

这篇关于Laravel 5无法将变量传递给所有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆