I am presently using below code in route in Laravel 8.
use App\Http\Controllers\Annonymous\Login\API\LoginAPIController;
Route::post('/authenticate', [LoginAPIController::class, "authenticateUser"])->name("apiAuthenticateUser");
Like there is name property, Is there any way to set middleware also for throttle along with name property? Something like this?
use App\Http\Controllers\Annonymous\Login\API\LoginAPIController;
Route::post('/authenticate', [LoginAPIController::class, "authenticateUser"])->name("apiAuthenticateUser")->throttle("10, 1");
Yes, throttle is a middleware so you would do the following
Route::post('/authenticate', [LoginAPIController::class, "authenticateUser"])->name("apiAuthenticateUser")->middleware('throttle:10, 1');
Related
I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.
i have some problem about routes in laravel
Route::get('aset/create', 'TransaksiController#aset_create');
Route::get('aset/{id}', 'TransaksiController#aset_view');
these routes was fine, but when i switch the position like
Route::get('aset/{id}', 'TransaksiController#aset_view');
Route::get('aset/create', 'TransaksiController#aset_create');
the aset/create was went to aset/view
whats happening?
thanks!
The aset/create will trigger the Route::get('aset/{id}', 'TransaksiController#aset_view'); as you can see the template displayed is for view aset-view.blade.php not the template for the create, so laravel identifies create as the id param so your db query will return no rows creating the error you see,
To fix this problem you either keep the original order or you change the view route to match only number(or not match create)
Route::get('aset/{id}', 'TransaksiController#aset_view')->where('id', '[0-9]+');
aset-view.blade.php file, that's where you have issue. You are either accessing a variable as object, it could be something null or array which you are accessing as an object
{id} is a parameter for passing it is receving now create as parameter.since it is in top.Rearrange will solve the problem.id will catch anything you pass
I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
Is it possible to to give same route names with different controller functions.
Then how ?
No, you can't.
If you write same route again, it will overwrite first one.
It is just like an array variable.
If you assign any other value to variable, first one will overwrite.
But you can specify HTTP method with route.
No you can't do like this, you are going to make api then define method that will help
$route["signup"]["post"] = "Controller/signup";
$route["signup"]["GET"] ="Controller2/fbsignup";
I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
then try this in routes and url should be like signup/signup and signup/fbsignup
$route["signup/signup"] = "Controller/signup";
$route["signup/fbsignup"] ="Controller2/fbsignup";
call the url like
signup/signup
signup/fbsignup
I have a particular custom route on my viewset, which is basically an action on the detail object with an extra parameter in the url (e.g., r'^{prefix}/{lookup}/sub_items/$'). I want to specify a different authentication_class for this route than the rest of the viewset. Where can I specify that? I see there are #list_route and #detail_route dectorators that can be used to specify that, but what about for custom routes?
Thanks.
I figured it out. The Route should set:
initkwargs={'authentication_classes': (authentication.MyCustomAuthenticationForThisRoute,)}
My application has the following module:
/groups/{id_group} -- > GroupController.php
My question is this,
/groups/{id_group}/events/{id_event}
Which controller should handle this?
If you are using groups/n/ as routing, you would want to use your framework's routing system to route to the event controller. Otherwise, it is the groups controller.
If you want to stick to just using the first parameter as the Controller, then consider using the further parameters as purely parts of the query string e.g
?controller=groups&itemId={id_group}&action=events&actionId={id_event)
and use the controller to route it to the correct place e.g.
$model->getGroup($id_group, 'events', $id_event);