控制器使用变量Laravel返回视图

时间:2017-07-31 01:47:42

标签: php laravel laravel-views

我有这个变量应该在我的网址中,但包含“。” (点)。对不起,我在laravel仍然是菜鸟。

预期结果是localhost / myProject / public / var_name

Eror表示未找到View [.sampleVariable]。

我的行是

return view('/'.$create->var_name)->compact('anotherVar','anotherVar');

我的路线是Route::get('{var_name}', 'MyController@index');

2 个答案:

答案 0 :(得分:0)

路线

Route::get('/{var_name}', 'MyController@index');

myController的

public function index($var_name)
{
    return view('template.index', ['var_name' => $var_name])->compact('anotherVar','anotherVar');

}

答案 1 :(得分:0)

尝试以下代码。 您的控制器功能代码如

public function index($var_name)
{
    //Initiate your variable...
    $anotherVar = '';

    //Replace 'BLADEFILENAME' to you want to execute blade file name...
    return view('BLADEFILENAME', compact('var_name','anotherVar'));
}

您可以阅读有关php compact()的更多信息。您还可以通过wrapping the variable in curly braces

将变量值从控制器传递到视图

您的路线代码

Route::get('/{var_name}', 'MyController@index');

现在您可以使用$var_name& $anotherVar放入您的刀片文件中。