2017-08-02 67 views
-1

我無法弄清楚如何在laravel的刀片模板中顯示路徑名稱。請在下面找到示例代碼。謝謝。無法在控制器的刀片模板中顯示變量

從控制器(StaffsController.php)

public function index() 
{ 
    $thisRoute = Route::current()->uri(); 
    return view('staff.list')>with(compact('thisRoute')); 
} 

刀片:

{{ $thisRoute }} 

這是的var_dump

/home/vagrant/Code/spark/app/Http/Controllers/StaffsController.php:20:string 'staffs' (length=6) 

錯誤:

(1/1) UnexpectedValueException 
The Response content must be a string or object implementing __toString(), "boolean" given. 

當我在控制器更改代碼:

public function index() 
{ 
    $thisRoute = Route::current()->uri(); 
    return dd($thisRoute); 

} 

我得到「員工」爲這是正確的輸出是從轉儲一個字符串,右?

+0

你期望打印什麼? – Laerte

+0

爲什麼在'dd'中使用'Route :: current() - > uri()',但是在第一個示例中只是'Route :: current();'? –

+0

也https://laravel.com/docs/5.2/routing#accessing-the-current-route –

回答

0

改變你的return語句是這樣的:

return view('staff.list', compact('thisRoute')); 

如果您正在使用laravel 5.3或以上,你可以得到路由名稱是這樣的:

Route::currentRouteName(); 

這一點,你真不」不得不從你的控制器中將它從視圖中直接使用:

{{ Route::currentRouteName() }} 
+0

對不起隊友,將代碼更改爲'$ thisRoute = Route :: currentRouteName(); ('thisRoute'));'但是stil得到了同樣的錯誤 –

+0

請改變你的return語句,像這樣︰return view('staff.list',compact('thisRoute 「)); – yoeunes

0

對不起,我解決了這個問題。我在該行 return view('staff.list')>with(compact('thisRoute'));

改變它錯過了-

return view('staff.list')->with(compact('thisRoute')); 

我犯了一個錯誤。

謝謝

+0

你也可以使用簡短的語法:'return view('staff.list',compact('thisRoute')); ' – yoeunes

+0

@yoeunes謝謝你,先生:) –

相關問題