404 未找到,但 Laravel 5.4 中存在路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43715106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:44:25  来源:igfitidea点击:

404 Not Found, but route exist in Laravel 5.4

laravellaravel-5phpstormlaravel-5.4

提问by Melvin Jeconiah

I'm using PhpStorm. I can run and open the index.php, but when I want to press submit button (post sign in), its display 404 not found.

我正在使用 PhpStorm。我可以运行并打开index.php,但是当我想按提交按钮(登录后)时,找不到它的显示 404。

Web server Apache 2.4 running on Windows 10.

在 Windows 10 上运行的 Web 服务器 Apache 2.4。

This is my home

这是我的家

index.php

索引.php

This is my route

这是我的路线

web.php

网页.php

回答by ChronoFish

I'm not entirely sure why all the down-votes, especially since this is a common issue and the cause can be hidden for someone new to the Laravel environment. I know this is an old post, but I add it here for future newbies.

我不完全确定为什么所有的否决票,特别是因为这是一个常见问题,对于 Laravel 环境的新手来说,原因可能会被隐藏。我知道这是一个旧帖子,但我在这里添加它以供将来的新手使用。

The first thing I suggest trying is running php artisan route:listfrom the command line (from your project root directory). This will list all the routes that Laravel can register and if there are any errors, usually they will show up here.

我建议尝试的第一件事是php artisan route:list从命令行(从您的项目根目录)运行。这将列出 Laravel 可以注册的所有路由,如果有任何错误,通常它们会显示在这里。

The second thing I suggest is to ensure that the URL matches route. I can't tell you how many times I've tried to figure out why my route was returning a 404 simply because my form was posting to something like /insertStudentwhen my route was defined as /admin/insertStudent

我建议的第二件事是确保 URL 与路由匹配。我无法告诉你有多少次我试图弄清楚为什么我的路线返回 404 只是因为我的表单发布到诸如/insertStudent我的路线被定义为/admin/insertStudent

The third thing I suggest is to ensure the method you are calling exists. Are your methods really called postSignInand postInsertStudentand not simply SignInand InsertStudent? Keep in mind that both the URL and method names are case sensitive and should match where you define the route, the URL that is being called, and the method that is receiving the request.

我建议的第三件事是确保您正在调用的方法存在。你的方法真的被称为postSignInandpostInsertStudent而不是简单的SignInandInsertStudent吗?请记住,URL 和方法名称都区分大小写,并且应该与您定义路由的位置、被调用的 URL 以及接收请求的方法相匹配。

Finally if all that checks out, one last thing I might suggest you try is running php artisan route:clear. This resets the route cache.

最后,如果所有这些都检查出来,我可能建议您尝试的最后一件事是运行php artisan route:clear. 这将重置路由缓存。

回答by Ram Chander

I had the same issue and fixed by following

我遇到了同样的问题并通过以下方式修复

I have edit .htaccess file

我已经编辑了 .htaccess 文件

RewriteEngine On
RewriteBase /path/of/project/folder/
# change above to your site i.e.,  RewriteBase /whatever/public/

Hope it will work for you

希望它对你有用

回答by Anthony Artemiev

Make sure you added correct Acceptheader.

确保您添加了正确的Accept标题。

In my case I tried to access apiendpoints and each time recieved 404 resource not found. When I've added Accept: application/jsonheader I got response from matched route

就我而言,我尝试访问api端点,但每次收到 404 资源都找不到。当我添加Accept: application/json标头时,我从匹配的路由中得到了响应

回答by Hamid

This is old question but is active yet. for windows users if you using CAPITALcharacters (upper-case letter) in your project folder you should using same in your browser. If your project is in "c:\xampp\htdocs\MyProject\"you should using MyProjectas url instead of myprojectfor example in the above project (MyProject) following route:

这是一个老问题,但还很活跃。对于Windows用户如果使用资本项目中的字符(大写字母)的文件夹,你应该用你的浏览器一样。如果您的项目在"c:\xampp\htdocs\MyProject\"您应该使用MyProject作为 url 而不是myproject例如在上面的项目 (MyProject) 以下路线中:

Route::get('/dashboard', function () {
    return 'welcome to dashboard!';
});

will work if you use:

如果您使用:

http://MyProject/dashboard

but is better using lowercase characters in directories

但最好在目录中使用小写字符

回答by Ruberandinda Patience

Please Make you have Apache configured with the following information

请使用以下信息配置Apache

in /path/to/apache2/installation/conf/httpd.conf

Add the following information

添加以下信息

<Directory "path/to/laravel/project/public">
    Allowoverride All
</Directory>

.htaccess file located in public folder make sure that it has the following

位于公共文件夹中的 .htaccess 文件确保它具有以下内容

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
       Options -MultiViews
   </IfModule>
   Options +FollowSymlinks
   RewriteEngine On

   # Redirect Trailing Slashes...
   RewriteRule ^(.*)/$ / [L,R=301]

   # Handle Front Controller...
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ index.php [L]
</IfModule>

Remember to enable mod_rewrite module

记得开启 mod_rewrite 模块

sudo a2enmod rewrite

回答by Ruhith Udakara

try

尝试

php artisan route:clear
php artisan route:cache

and then type this and see whether your route exists in the list

然后输入这个,看看你的路线是否存在于列表中

php artisan route:list

and also in Middleware\VerifyCsrfToken.php check post routes are allowed

并且在 Middleware\VerifyCsrfToken.php 中也允许检查发布路由


class VerifyCsrfToken extends Middleware {

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*'
    ];

}

回答by Kayal

make sure, the url passed is equal in your route. check the parameters and action in the form. To get clear answer post your mvc

确保传递的 url 在您的路线中相等。检查表单中的参数和操作。要获得明确的答案,请发布您的 mvc

回答by david valentino

dontforget, the url is sensitive case to the folder name

不要忘记,url 是文件夹名称的敏感大小写

回答by Iago F.

I had the same issue recently and I thought I was using a Reserved word, which I couldn't find in the list of reserved words of PHP.

我最近遇到了同样的问题,我以为我使用了保留字,但在PHP 的保留字列表中找不到。

Originally my Routes/web.phpwas:

最初我的Routes/web.php是:

Route::post('sysinfo/store',['as'=>'sysinfo.store', 'uses'=>'SysinfoController@store']);

After I changed to another name as shown bellow, it worked!

在我更改为如下所示的另一个名称后,它起作用了!

Route::post('newsysinfo/store',['as'=>'newsysinfo.store',   'uses'=>'SysinfoController@store']);

What's weird is that I have other working routes such as:

奇怪的是我还有其他工作路线,例如:

Route::get('sysinfo/',['as'=>'sysinfo.index',   'uses'=>'SysinfoController@index']);
Route::post('sysinfo/{sysinfo}',['as'=>'sysinfo.update',   'uses'=>'SysinfoController@update']);
Route::get('sysinfo/create',['as'=>'sysinfo.create',   'uses'=>'SysinfoController@create']);
Route::get('sysinfo/{id}/edit',['as'=>'sysinfo.edit',   'uses'=>'SysinfoController@edit']);
Route::get('sysinfo/{sysinfo}/destroy',['as'=>'sysinfo.destroy',   'uses'=>'SysinfoController@destroy']);

Although it's not an explanation of the causes of the problem, perhaps this workaround can help you.

虽然这不是对问题原因的解释,但也许此解决方法可以帮助您。

回答by Luke Madhanga

Don't forget the order of your routes. For example, if you want to go to /path/secondand have the following routes registered:

不要忘记路线的顺序。例如,如果您要前往/path/second并注册以下路线:

Route::get('/path/{dynamic_second}', 'Controller@actionDynamic');

Route::get('/path/{dynamic_second}/{dynamic_third}', 'Controller@third');

Route::get('/path/second', 'Controller@action');

This will break, as secondis consumed by /path/{dynamic_second}.

这将打破,如second被消耗/path/{dynamic_second}

If you want to access /path/second, you have to have your routes listed like so:

如果你想访问/path/second,你必须像这样列出你的路线:

Route::get('/path/second', 'Controller@action');

Route::get('/path/{dynamic_second}', 'Controller@actionDynamic');

Route::get('/path/{dynamic_second}/{dynamic_third}', 'Controller@third');