New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
authorizeResource does not work with CamelCase models #22493
Comments
Provide complete code examples and error messages for reproduction. It could just be a missing using-statement in this case. |
I just set up a quick app to reproduce and everything worked as I expected. // web.php
Route::resource('user_posts', 'UserPostsController');
Example with named parameter: // web.php
Route::resource('posts', 'UserPostsController', ['parameters' => [
'posts' => 'user_post'
]]); //UserPostPolicy.php
public function view(User $user, UserPost $user_post)
{
return $user->id === $user_post->user_id;
} // AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Gate::resource('user_post', 'UserPostPolicy');
} // UserPostsController.php
public function __construct()
{
$this->authorizeResource(UserPost::class);
}
public function show(UserPost $user_post)
{
return $user_post;
} Now visit I'll qualify this by saying that on the rare occasion I've needed a multi-word model, this is how I've always named everything. Probably because when I first tried, this is how I was able to get |
Thanks for your input @devcircus but still I'm not able to fix the issue. I've used the policy mapping in Route::resource('customer-requests', 'CustomerRequestController', [
'names' => [
'index' => 'customer_requests'
]
]); I'll try your suggestions on a fresh Laravel app and will see how it behaves. Thanks again for your input. |
He didn't change route names, he changed the route parameter names. |
I'll post a link to the repo shortly. |
@devcircus Thank you! |
I am experiencing this same issue. Did someone find a solution to this problem? |
This is the temporary solution, that solved it for me:
Use the second parameter of |
So I have come to this issue too. Mainly it is issue with naming conventions and some automatic transformations. It works well if you do it as intended, but it gets ugly when you diverge a bit. It is important to keep in mind that:
expect parameter from the route named the same as in the function. But camel case works for snake case meaning route parameter something_long works for controller parameter $somethingLong
Gets class name, converts it to snake case and expect route parameter of this name. So if your model class is named newsPost it expects route parameter named news_post. This can be overriden by supplying parameter name like this
So if you use route resource name news-posts, it will all work. If you use newsPosts or something else, perhaps localized, you have to change the parameter name to the expected snake case model name.
|
you must put route parameter in snake case
and parameter in camel case
|
Description:
Laravel resource policies work perfectly for generally named models but with CamelCased names, they don't.
Steps To Reproduce:
In my controller's constructor:
This doesn't work and always return unauthorized access:
For a generally named model, it works:
p.s.: The fixes suggested for the earlier versions of Laravel don't work and I already have tested.
The text was updated successfully, but these errors were encountered: