Laravel - How to recover the values of a form and redirect using the Route class?

Asked

Viewed 4,889 times

0

I am studying Laravel and at the moment I am trying to understand how to send values of forms to be recovered and then redirect to a specific page. I’m still starting with Laravel and I don’t even 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 doubts:

  1. I don’t know if this is the right place to back up the form data
  2. I don’t know if this is the right way to go.

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 looked into that page

2 answers

4


Recovers the data on Controller!

In his View on the form would look like this:

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

In the archive Routes.php:

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

And last in his 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 class Request must declare: use Illuminate\Http\Request; at the top of his Controller

0

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

I will make some changes to your code and make it as clear as possible so that you understand a basic flow of requests and responses involving parameters. Modify your view so it stays this way (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>

Then we will make this route respond with a method of a controller, make the following changes:

Http app Routes.php

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

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

Http Controllers app Examplecontroller.php

..

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

That way, you’ll notice that when the form is sent, our route leads us to the method action of example controller, which then redirects us back to the form using the 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

displays the previous parameter with the username name.

  • Good explanation but from what I understood it not that go back to the form page but rather catch the user and check who it is, and then redirect to the right page!

  • Thanks for the comment @Igormello, I simply wanted to show the path of the parameters in a simple way, so he himself can create the logic and apply it to your needs. I also put an answer to the main problem, which in case is invalid route.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.