使用 Laravel 4 在视图中访问路由参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21971742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:05:04  来源:igfitidea点击:

Accessing route parameter in view with Laravel 4

phplaravellaravel-4url-routing

提问by John Dorean

I have a route set up like so:

我有一个这样设置的路线:

Route::match(array('GET', 'POST'), '/reset-password/{code}', array('as' => 'reset-password-confirm', 'uses' => 'UserController@resetPasswordConfirm'));

In my controller, I'm passing the route parameter to my action like so:

在我的控制器中,我将路由参数传递给我的操作,如下所示:

public function resetPasswordConfirm($code)
{
    // ...
}

I can then use $codein my controller as normal.

然后我可以$code像往常一样在我的控制器中使用。

In my view I'm building a form which POSTs to the same controller action, and I need to somehow get $codeinto the view so it constructs the proper form action. At the moment I have this:

在我看来,我正在构建一个向同一个控制器操作发送 POST 的表单,我需要以某种方式$code进入视图,以便它构建正确的表单操作。目前我有这个:

{{ Form::open(array('route' => array('reset-password-confirm'))) }}

Because I'm not supplying the $coderoute parameter, the form is opened like this:

因为我没有提供$code路由参数,所以表单是这样打开的:

<form method="POST" action="http://site.dev/reset-password/%7Bcode%7D" accept-charset="UTF-8">

Obviously, this doesn't match the route I've defined (due to {code}not existing) and route matching fails. I need to somehow get the route parameter into my view so I can use it with Form::open(). I've tried doing this:

显然,这与我定义的路由不匹配(​​由于{code}不存在)并且路由匹配失败。我需要以某种方式将路由参数放入我的视图中,以便我可以将它与Form::open(). 我试过这样做:

{{ Form::open(array('route' => array('reset-password-confirm', $code))) }}

But that just throws an exception saying $codeis undefined.

但这只是抛出一个异常,说$code是未定义的。

回答by The Alpha

The proper way to send the parameterto the view is:

将 发送parameter到视图的正确方法是:

return View::make('viewname')->with('code', $code);

Or you may use:

或者你可以使用:

return View::make('yourview', compact('code'));

So, the $codewill be available in your view and you may use it in your form but you may also use following approach to access a parameterin the view:

因此,$code将在您的视图中可用,您可以在表单中使用它,但您也可以使用以下方法parameter在视图中访问 a :

// Laravel - Latest (use any one)
Route::Input('code');
Route::current()->getParameter('code');
Route::getCurrentRoute()->getParameter('code');

// Laravel - 4.0
Route::getCurrentRoute()->getParameter('code');

回答by Damien Pirsy

Maybe I don't clearly understand your question, but you can pass it to the view regularly (as you would pass any other variable, i.e.)

也许我不太明白你的问题,但你可以定期将它传递给视图(就像你传递任何其他变量一样,即)

public function resetPasswordConfirm($code)
{
    return View::make('yourview')->with('code', $code);
}

and in the view $code will be defined:

并在视图中 $code 将被定义:

{{ Form::open(array('route' => array('reset-password-confirm', $code))) }}

orjust catch it in your view directly from the Request object:

或者直接从 Request 对象在您的视图中捕获它:

{{ Form::open(array('route' => array('reset-password-confirm', Request::segment(2) ))) }}

By the way, I think you could write your route also like this:

顺便说一句,我认为你也可以这样写你的路线:

Route::any('reset-password/{code}', array('as' => 'reset-password-confirm', 'uses' => 'UserController@resetPasswordConfirm'));