简体   繁体   English

在Laravel找不到页面,路线问题

[英]Page not found in Laravel, routes problem

I have a problem with my Laravel and probably routes but I'm not sure. 我的Laravel和可能的路线有问题,但我不确定。

So, there is a contact form in modal window. 因此,模态窗口中有一个联系表单。 When I click on Send button I've got page not found error. 当我点击Send按钮时,我找不到页面错误。

What I have in the web.php is 我在web.php有什么

Route::post('/apply_now','HomeController@apply_now')->name('apply_now');

In the HomeController.php 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. 在此服务器上找不到请求的URL / apply_now。

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 更新2.模态

<!-- 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">&times;</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

enter image description here 在此输入图像描述

Did you see the area in the picture? 你看到图片中的区域了吗?

Try Adding 尝试添加

{{ csrf_field() }} 

This will add the CRSF Token field to your form 这会将CRSF令牌字段添加到表单中

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 如果laravel需要,CSRF默认在所有路由上启用,以检查发布请求是否安全,您也可以从VerifyCsrfToken.php中禁用它,该中间件位于

app\Http\Middleware 

To Disable the CRSF for your route update 为路由更新禁用CRSF

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. 在你的形式.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 现在,一旦您提交表单,laravel将检查是否与表单一起发送了crsf令牌,并让您的请求继续进行

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: 并确保您已将两个URL定义到web.php文件中,如下所示:

Route::post('/','HomeController@index');
Route::post('apply_now','HomeController@apply_now')->name('apply_now');

and make sure your server has turned on mod rewrite. 并确保您的服务器已打开mod重写。 So Laravel can handle .htaccess rules. 所以Laravel可以处理.htaccess规则。

Try with this, 试试这个,

Route::get('/', 'HomeController@index')->name('home');

In your controller 在你的控制器中

return redirect()->route('home')->with('message', 'Sent');

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM