Laravel: Get the name of the view that called the controller method

前端 未结 2 1096
忘掉有多难
忘掉有多难 2021-01-28 18:24

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

相关标签:
2条回答
  • 2021-01-28 18:58

    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
    
    0 讨论(0)
  • 2021-01-28 19:16

    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').

    0 讨论(0)
提交回复
热议问题