问题
In laravel is it possible to get the name of the view that called the controller method that you're currently in?
I have two version of a form in my site. One is meant to be used "outside" of the site (i.e. the user is not logged in) and the other is meant to be used "inside" of the site. The forms differ a bit in presentation, but they hold all of the same fields and call the same store method.
Is there a way from within my store method to get the name of the view that called the method? I looked through the docs but didn't see a clear way of doing so.
回答1:
If it's a form why not just store the view name in a hidden input and send it along with the rest of the form data.
Form::hidden('view', 'model.index')
I don't think there's a way to get the current view name but you can pass the view name to the view from the originating controller when you set the view to use in the first place:
public function create()
{
$myView = 'model.index';
Return View::make($myView)->with(compact('myView'));
}
Then the $myView
variable containing the view name is available for inserting in the form, as described above.
In the receiving controller (your store
method) just retrieve it from the form data with Input::get('view')
.
回答2:
This is how you can achieve that. Just do this in the controller method where you want to detect the previous route name from which came the request.
$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
if($route == 'route name here') {
return redirect()->back(); //example
}
return view('users.index'); //example
来源:https://stackoverflow.com/questions/24040886/laravel-get-the-name-of-the-view-that-called-the-controller-method