简体   繁体   English

Laravel NotFoundHttpException尽管路由存在

[英]Laravel NotFoundHttpException although route exists

I use vue.js and Laravel 5.1 to create a little file sharing application. 我使用vue.js和Laravel 5.1来创建一个小文件共享应用程序。

Everything works perfect but now I wanted to make sure the owner of each file is able to remove users from his file (he had to share the file with those users at first of course), therefore I make a PUT request to an URL called /files/share . 一切都很完美但现在我想确保每个文件的所有者能够从他的文件中删除用户(他当然必须首先与这些用户共享文件),因此我向一个名为/files/share的URL发出PUT请求/files/share

My Laravel route looks like this: 我的Laravel路线如下:

Route::put('/files/share', 'FileController@test');

When I run php artisan route:list it gets listed as well. 当我运行php artisan route:list它也会被列出。

The client-side code looks like this: 客户端代码如下所示:

this.$http.put('/files/share', { some_data }, function(data) {
    if(data.error){
        this.$set('error', data.error);
    } else {
        this.$set('file', data);
    }
});

The exact error that I get is this: 我得到的确切错误是这样的:

2/2 NotFoundHttpException in Handler.php line 46:
No query results for model [App\File].
1/2 ModelNotFoundException in Builder.php line 129:
No query results for model [App\File].

But the Application doesn't even get to the controller, if I just return something from there the error is the same. 但应用程序甚至没有到达控制器,如果我只是从那里返回一些错误是相同的。

With Laravel routes, the order matters. 通过Laravel路线,订单很重要。 Routes with dynamic segments like files/{file} or resource routes should always be defined after the ones that are static. 具有动态段(如files/{file}或资源路径)的路由应始终在静态段之后定义。 Otherwise Laravel will interpret the share part in your URL as ID. 否则,Laravel会将您网址中的share部分解释为ID。

So, as you've figured out yourself you simply need to change the order of your routes: 所以,正如你自己想出的那样,你只需要改变路线的顺序:

Route::put('/files/share', 'FileController@test');
Route::resource('/files', 'FileController');

感谢lukasgeiter,我再次检查了我的路由,并且必须在我的RESTful资源路由之前定义/ files / share路由。

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

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