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

     .
     .
     .
}

图片转代码服务由CSDN问答提供 功能建议

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

这是代码:

  namespace App \ Http \ Controllers; 
 
use App \ Http  
equests; 
use App \ Http \ Controllers \ Controller; 
use Sentry; 
use Illuminate \ Http \ Request; 
use View; 
 
class BaseController extends Controller {
 public function __construct(){
 $ obavijesti =  “其他数据”; 
 $ izbornici =“一些数据”; 
查看::分享('izbornici',$ izbornici); 
查看::分享('obavjesti',$ obavjesti); 
} 
  } 
 
class AdminController扩展了BaseController {
。
。
。
 echo'&lt; pre&gt;'; var_dump($ izbornici); echo'&lt; / pre&gt;'; //错误弹出窗口\  n 
。
。
。
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题