Laravel NotFoundHttpException虽然路由存在 [英] Laravel NotFoundHttpException although route exists

查看:203
本文介绍了Laravel NotFoundHttpException虽然路由存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用vue.js和Laravel 5.1来创建一个小的文件共享应用程序。

I use vue.js and Laravel 5.1 to create a little file sharing application.

一切都很完美,但现在我想确保每个文件的所有者能够从他的文件中删除用户(他必须先与这些用户共享文件当然),因此我向一个名为 / files / share 的URL发出一个PUT请求。

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.

我的Laravel路由看起来像这样:

My Laravel route looks like this:

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

当我运行 php artisan route:list 它也被列出。

客户端代码如下所示:

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

我得到的确切错误是:

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.

推荐答案

使用Laravel路线,顺序很重要。具有动态段(如 files / {file} 或资源路由)的路由应始终在之后定义为静态的路径。否则,Laravel将将您的URL中的共享部分解释为ID。

With Laravel routes, the order matters. Routes with dynamic segments like files/{file} or resource routes should always be defined after the ones that are static. Otherwise Laravel will interpret the share part in your URL as 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');

这篇关于Laravel NotFoundHttpException虽然路由存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆