2017-08-02 117 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谢谢你,先生:) –

相关问题