I wondered if there is a way to change the default URLs of Laravel's resource controller. For example, for basic CRUD operation, for creating, we have a /create route made by default by Laravel. Can it be changed to /ask or /new or something like that?
You can "localize" the resource URIs that are created without much work (Added to the boot method of a Service Provider):
Route::resourceVerbs([
'create' => 'new',
]);
This would have all calls to Route::resource(...) create the URI with 'new' instead of 'create' for the create action.
If you need to get more complicated than something like that you could extend Illuminate\Routing\ResourceRegistrar to override it in any way you would like. You could call an instance of your version or bind it to the container for Illuminate\Routing\ResourceRegistrar which would use it for all resource calls.
Laravel 9.x - Docs - Controllers - Resource Controllers - Localizing Resource URIs
Related
The API endpoint /clients/entries returns a 404 error while using the route setup.
Route::apiResource('clients', ClientController::class);
Route::get('clients/entries', [ClientController::class , 'getAll']);
The endpoint only works when they are rearranged, so the resource route is at the end.
Route::get('clients/entries', [ClientController::class , 'getAll']);
Route::apiResource('clients', ClientController::class);
Why does this issue occur? And is it fine to have the resource route at the end?
A full explanation can be found at https://stackoverflow.com/a/62952620/9004987 (thanks to #Espresso).
Summary:
When the resource route is registered at the beginning it will create some routes. Example:
GET /clients/{client} show clients.show
And when other custom routes (such as /clients/entries) are registered after the resource route then it will conflict with the resource URI (since it has the same pattern).
Solution:
Define the custom routes before the resource route.
Firs:
You need to direct requests from Routes/web.php to API
Route::get('/{any}', [App\Http\Controllers\ClientController::class, 'index'])->where('any','.*');
Requests from routes/api.php need to be answered
im using a package called https://github.com/garygreen/pretty-routes
there is line in its service provider boot() method (here the code)
it is defining a get route with middlewares from its config file(link to the code) I just added 'auth:web' to its config file but it seems the 'auth:web' middleware is called as soon as code reaches the line before Laravel bootstraps its session and etc. when the auth('web')->user() is yet null
What I can not understand is that I do the same exact thing (here the code)with laravel/telescope but it works. why ???
also changing :
Route::get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
to :
$this->app['router']->get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
in service provider seems to solve the problem and make this code behave like the way telescope package use 'auth:web' as middleware.
what's happening ?
You need to have the web middleware applied to any routes you need sessions for, which is what the default authentication system is using. When you apply the auth middleware without this it can't possibly resolve a user since there is no session to be authenticated against.
You need to apply the web middleware and then what ever other middleware you want:
'middlewares' => [
'web', 'auth:web',
],
If you look at the telescope example you provided you will see they also add the web middleware. So you didn't quite "do the same exact thing" as the telescope config.
Laravel 5.5
which is the different doing (no get and post methods) on route defintion web.php file:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController#login');
regarding ->name('') method
Is required define that method? in which cases?
(sample taked from Auth Class definition laravel)
The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.
Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').
If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.
I'm using Laravel to build and api based admin control application and I'm using
Route:resource instead of regular GET and POST methods.
Just realized my hosting provider DOESN'T ALLOW PUT and DELETE now I need to so now I have to use POST and GET methods.
this is what i have
Route::resource('contacts', 'Admin\\ContactInfoController',['only' => ['create', 'store', 'update']]);
Route::get('claims/statuses', 'Admin\\ClaimsController#statusCodes');
Route::get('claims/costcenters', 'Admin\\ClaimsDetailsController#getCostCentres');
Route::get('claims/{id}/details', 'Admin\\ClaimsController#details');
Route::get('claims/{id}/messages', 'Admin\\ClaimsController#messages');
Route::resource('claims', 'Admin\\ClaimsController',['only' => ['index','store','update','destroy','edit']]);
Route::resource('claims/details', 'Admin\\ClaimsDetailsController',['only' => ['store','update','destroy']]);
What approach might be best in converting my routes from PUT and DELETE to POST and GET?
I don't think it's possible that your hosting provider doesn't allow to put or delete request. If you created API it could be the case but in other cases (you created normal page) you send forms using POST method with hidden field _method set to HTTP verb, so if only your provider supports POST method it will work without a problem. You can read Form method spoofing section about this.
Contact your hosting provided to allow put or delete request, it's part of web development and this is quite limiting.
I am new to Laravel and learning just basic stuff. I am trying to create a new Route to create a user into database. So I created a Route like this -
Route::get('users/"create"', 'PagesController#create');
But I also have one Route for users trying to access their profile-
Route::get('users/{username}', 'PagesController#show');
Now when I try to access users/create it redirect me to show method instead of create method in controller. I am guessing this is because of generic nature of users/{username}. So my question is how to deal with such situation.
The order that you define routes is important. If you define your routes like this - in this order, it will work.
Route::get('users/create', 'PagesController#create');
Route::get('users/{username}', 'PagesController#show');
Note - I noticed you used 'users/"create"' - that is an error - it should be 'users/create' like in my example.
p.s. make sure you dont allow a user with the username called 'create' - or they will never be able to get to their profile page.