I have a problem with my Laravel and probably routes but I'm not sure.
So, there is a contact form in modal window. When I click on Send
button I've got page not found error.
What I have in the web.php
is
Route::post('/apply_now','HomeController@apply_now')->name('apply_now');
In the HomeController.php
public function apply_now(Request $request)
{
... form fields data
return Redirect::to('/')->with('message', 'Sent');
}
And the form
{{Form::open(array('route'=>'apply_now','files' => true,'method'=>'post'))}}
...
form field
{{Form::close()}
The error
Not Found
The requested URL /apply_now was not found on this server.
I don't see anything wrong with the routes but yet can't find the problem.
UPDATE:
| | POST | apply_now | apply_now | App\Http\Controllers\HomeController@apply_now
UPDATE 2. The modal
<!-- Apply Modal -->
<div class="modal fade" id="apply" tabindex="-1" role="dialog" aria-labelledby="applyModalLable">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="applyModalLable">Apply Now</h4>
</div>
<div class="modal-body">
<form method="POST" action="https://example.com/apply_now" accept-charset="UTF-8" enctype="multipart/form-data">
<input name="_token" type="hidden" value="OyEdnHIWRgbZmPo0joodNmWraDSuuACIrwqup044">
<div class="form-group ">
<input type="text" name="your_name" class="form-control" placeholder="*Your Name" value="" >
</div>
<div class="form-group ">
<label>*Country</label>
<input type="text" name="country" class="form-control" placeholder="Your Country" >
</div>
<div class="form-group ">
<input type="text" name="contact_email" class="form-control" placeholder="*Contact Email" >
</div>
<div class="form-group ">
<input type="text" name="contact_phone" class="form-control" placeholder="*Contact Phone">
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-custom btn-sm btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
制作这样的路线:
Route::match(['get', 'post'],'/apply_now', 'HomeController@apply_now');
Looks like there is no error in the code, can you clear the cache;
php artisan route:cache
Did you see the area in the picture?
Try Adding
{{ csrf_field() }}
This will add the CRSF Token field to your form
eg. <input type="hidden" name="_token" value="SomeRandomString">
which is required by laravel ,CSRF is enabled by default on all Routes to check if the post request is secure , you can also disable it from VerifyCsrfToken.php which is middleware located at
app\Http\Middleware
To Disable the CRSF for your route update
protected $except = [
//
'apply_now'
];
Disabling this is not a good practice, If you want your application secure
Add
{{ csrf_field() }}
In your form for.eg.
{{ Form::open(array('route'=>'apply_now','files' => true,'method'=>'post')) }}
...
form field
{{ csrf_field() }}
{{ Form::close() }}
Now once you submit the form laravel will check if crsf token is sent with the form and let your request proceed further
Run this command and try again
php artisan optimize:clear
or
remove all files from the
/bootstrap/cache
/storage/framework/cache/data
/storage/framework/sessions
/storage/framework/views
and make sure you have defined both urls into web.php file something like this:
Route::post('/','HomeController@index');
Route::post('apply_now','HomeController@apply_now')->name('apply_now');
and make sure your server has turned on mod rewrite. So Laravel can handle .htaccess rules.
Try with this,
Route::get('/', 'HomeController@index')->name('home');
In your controller
return redirect()->route('home')->with('message', 'Sent');
Hope this helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.