Laravel - How to retrieve values from a form and redirect using the Route class?

-1

I am studying Laravel and at the moment I am trying to understand how to send form values to be retrieved and then redirect to a specific page. I'm still starting with Laravel and I do not know if that's how it's done.

So let's say I have the following form:

<form action="action_page.php">
  Username: <input type="text" name="userName"
  <input type="submit">
</form>

And here I have 2 questions:

  • I do not know if this is the correct place to retrieve form data
  • I do not know if this is the right way to do it.
  • In laravel I have the following route:

    Route::post('/action_page',function(){
        $userName = Input::get('userName');//Não sei se isso está correto. 
        if($userName=='paulo'){
            retur view('pagina_paulo');
        }
        elseif($userName=='jose'){
            retur view('pagina_jose');
        }
    
    
    });
    

    This is the error that appears:

    methodNotallowedhttpexception in routecollection.php ...
    

    I researched this page

        
    asked by anonymous 13.07.2016 / 17:19

    2 answers

    2

    Retrieves data in Controller !

    In your View on the form it would look like this:

    <form action="/action_page" method="post">
       ...
    

    In the routes.php file:

    Route::post('/action_page', 'SeuController@redireciona');
    

    And lastly on your SeuController :

    public function redireciona(Request $request)
    {
        //Recupera o userName do input
        $usuario = $request->input('userName');
         ...
    }
    

    I used the object Request that gets an instance of the current HTTP request.

    Obs: To use the Request class you must declare: use Illuminate\Http\Request; on top of your Controller

        
    13.07.2016 / 17:55
    0

    In summary, the error happens because you are trying to send a post to the /action_page.php (defined in your form) route, and you only define the /action_page route.

    I'll make some changes to your code and leave it as clear as possible for you to understand a basic flow of requests and responses involving parameters. Modify your view so that it stays this way (I recommend removing the route extension in the form action):

    @if (Input::has('username'))
        <p>O valor antigo do campo username é {{ Input::old('username') }}.</p>
    @endif
    
    <form action="/action">
        <label for="username">Username</label>
        <input type="text" name="username">
        <input type="submit">
    </form>
    

    Next, let's make this route respond with a method of controller , make the following changes:

    app \ Http \ routes.php

    Route::post('/action', 'ExampleController@action');
    

    Now we need a controller, you can create it via the command line with php artisan make:controller ExampleController or manually, leave it this way:

    app \ Http \ Controllers \ ExampleController.php

    ..
    
    class ExampleController extends Controller
    {
        public function action(Request $request)
        {
            return redirect()
                ->back()
                ->withInput($request->all());
        }
    }
    

    In this way, you will notice that when the form is submitted, our route directs us to the action method of example controller , which then redirects us back to the form using redirect()->back() . This specific part of the code:

    ..
    
    ->withInput($request->all())
    
    ..
    

    causes the previous (form) parameters to be sent along with the redirect (to the form page again), and this other specific part (of the form):

    @if (Input::has('username'))
        <p>O valor antigo do campo username é {{ Input::old('username') }}.</p>
    @endif
    

    shows the previous parameter with the username.

        
    13.07.2016 / 21:27