How do I set authentication_classes on custom route? - django-rest-framework

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,)}

Related

Set Throttle property with name property in single route

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');

optional additional parameters on resource route - laravel

I had searched for how to add parameters to resource route
Route::resource('posts','PostsController');
// became
Route::resource('posts/category.post','PostsController');
now , by the category.post I can declare additional parameters to all resource routes
but they are required , my question is how to make them optional ?
I tried something like this
Route::resource('posts/category?.post','PostsController');
to make the category parameter be an optional one , but that didn't work with me .
how I can do so ?
thank you .
You can try this, not sure though..
Route::resource('posts', 'PostsController')->except(['store' ]);
Route::post('posts/category', 'PostsController#store');
Resource route is not just a "route"
You may see it as a route group, but it is predefined and can easily be implemented when you have a normal resource controller
If you want to change the parameters you'll have to define the routes individually
Then you can make the parameters optional as needed
Route::post('/posts/category/{post?}, 'PostsController#store');
See the following docs
https://laravel.com/docs/7.x/routing#parameters-optional-parameters

Product And Category Separation In Route (Laravel)

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.

codeigniter with same route name with different controller functions

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

MVC subController

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);

Resources