简体   繁体   English

Laravel路由,这是预期的行为吗?

[英]Laravel routing, is this the expected behavior?

Not actually a problem, I think I can handle, but I want to know if this is behaving as expected. 我想我可以解决,但实际上不是问题,但是我想知道这是否符合预期。

I have a database table named users , then I created the Model and the UserController as a resource with 我有一个名为users的数据库表,然后我将Model和UserController创建为资源

php artisan make:controller UserController --resource

It created the expected routes (index, store, create, show, update, destroy and edit), some GET some POST. 它创建了预期的路线(索引,存储,创建,显示,更新,销毁和编辑),其中一些会获得一些POST。

So now I want to create a new route that gives me the user's avatar (an image I previously stored using the Storage methods) 因此,现在我想创建一条新路径,为我提供用户的头像(我之前使用Storage方法存储的图像)

So I create it in the /routes/web.php file as: 因此,我在/routes/web.php文件中将其创建为:

Route::get('/user/avatar/{id?}', 'UserController@avatar')->name('user.avatar');

I know the function works, if I go to http://localhost/user/avatar/1 it returns the image associated to user 1. Now the thing is, I want the parameter to be optional (nullable), if I don't get the id then I'll serve the \\Auth::user()->id image. 我知道该函数有效,如果我转到http:// localhost / user / avatar / 1,它将返回与用户1相关的图像。现在,如果我不希望该参数为可选(可空), t获取ID,然后我将提供\\Auth::user()->id图像。

The problem is whwn I go to http://localhost/user/avatar/ it gaves me an error (I won't print it here, because it isn't related), it looks like it is trying to go to the GET's /user/{id} route that was created with the resource, and it is treating "avatar" as the {id}. 问题出在我去http:// localhost / user / avatar /时,它给了我一个错误(我不会在这里打印它,因为它不相关),它似乎正在尝试转到GET的使用资源创建的/ user / {id}路由,并将“头像”视为{id}。

So I know, I should take out the line Route::resource('user', 'UserController'); 所以我知道,我应该取出Route::resource('user', 'UserController'); and create each route individually, right? 并分别创建每个路线,对吗?

My question is, is this how it is supposed to work? 我的问题是,这应该如何工作? should I create other, say, HelperController where I could point an /avatar/{id?} route? 我是否应该在可以指向/ avatar / {id?}路由的位置创建其他HelperController?

EDIT: The artisan route:list output regarding to user is: 编辑:工匠路线:关于用户的列表输出是:

|        | GET|HEAD  | user                   | user.index       | App\Http\Controllers\UserController@index                              | web          |
|        | POST      | user                   | user.store       | App\Http\Controllers\UserController@store                              | web          |
|        | GET|HEAD  | user/avatar/{id?}      | user.avatar      | App\Http\Controllers\UserController@avatar                             | web          |
|        | GET|HEAD  | user/create            | user.create      | App\Http\Controllers\UserController@create                             | web          |
|        | GET|HEAD  | user/{user}            | user.show        | App\Http\Controllers\UserController@show                               | web          |
|        | PUT|PATCH | user/{user}            | user.update      | App\Http\Controllers\UserController@update                             | web          |
|        | DELETE    | user/{user}            | user.destroy     | App\Http\Controllers\UserController@destroy                            | web          |
|        | GET|HEAD  | user/{user}/edit       | user.edit        | App\Http\Controllers\UserController@edit                               | web          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

Error is: 错误是:

 ErrorException (E_ERROR)
Trying to get property 'name' of non-object (View: D:\Dropbox\_www\reco\resources\views\user\profile.blade.php)
Previous exceptions

    Trying to get property 'name' of non-object (0)

This is in a $user->name call, $user is undefined, and I know what causes it, it is the find($id) inside show (in the Controller) which is getting nothing, because it is searching for "avatar" as id. 这是在$user->name调用中, $user是未定义的,我知道是什么原因,这是show(在Controller中)里面的find($id)什么都没有得到的,因为它正在搜索“头像” ”作为ID。

In fact, if I change the find() for findOrFail() it gives me the expected 404 error. 实际上,如果我将find()更改为findOrFail()它会给我预期的404错误。 So I'm pretty sure that it is interpreting avatar as the id. 因此,我很确定它将ID解释为头像。

You have to define your most explicit routes first: 您必须首先定义最明确的路由:

Route::get('/user/avatar/{id?}', 'UserController@avatar');
Route::resource('user', 'UserController');

Why? 为什么? Because route resource creates a GET user/{user} route and if you register resource route first, Laravel will look at the url user/avatar/1 and think that the avatar part is actually the ID of user. 因为路由资源会创建GET user/{user}路由,并且如果您先注册资源路由,则Laravel将查看url user/avatar/1并认为avatar部分实际上是user的ID。

It's just a quirk of laravel. 这只是laravel的怪癖。

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

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