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.