How to disable throttle in specific routes? (laravel) - php

Can we disable the laravel's throttle in a specific group of routes?
Here are my codes:
Route::group(['middleware' => ['auth:sanctum']], function () {
Route::get('/sample1', 'SampleController#sample');
// more routes here
});
I want to disable throttle limiter in all routes wrap inside Route::group. Can we possibly do that?

If you are using Latest laravel version then you disable throtle for specific route group.
You can exclude throttle:api middleware for specific route group using excluded_middleware
Route::group([
'middleware' => ['auth:sanctum'],
'excluded_middleware' => 'throttle:api'], function () {
Route::get('/sample1', 'SampleController#sample');
});

Related

Load backend and frontend routes separately without interfering

I have two kind of routes, admin routes and frontend routes.
The frontend routes
Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController#home']);
Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#index']);
Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#detail']);
The backend routes
Route::prefix('admin/dashboard')->group(function () {
Route::get('/', 'DashboardController#index')->name('dashboard');
});
Now when i type admin/dashboard or api/admin, laravel uses the frontend routes to load the views, while i want the backend views to be loaded.
So to filter out the backend routes i tried this
Route::group(['where' => ['page' => '^(?!admin|api)$', 'template' => '^(?!admin|api)$']], function ({
Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController#home']);
Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#index']);
Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#detail']);
});
which obviously did not work
Also the frontend routes should not have something like /website, they should all start with /
My question is: How can i load the backend and frontend routes separately without interfering when called, even if they have the same url length in terms of parameters, keep in mind that the admin routes always start with /admin or /api.
Note: i can't put the backend routes before the frontend routes
Thanks in advance!
If you want to you could put a constraint on the locale route parameter:
Route::pattern('locale', '^(?!(api|admin)$)(\w*)');
You can put this in the boot method of you RouteServiceProvider and it will now not allow the locale route parameter to match for 'api' or 'admin'.
You can register seperate routes in RouteServiceProvider. Following is how you can do it.
Inside RouteServiceProvider.php do:
public function map()
{
$this->mapFrontendRoutes();
$this->mapAdminRoutes();
}
Definition of mapFrontendRoutes():
protected function mapFrontendRoutes()
{
Route::prefix('{locales?}')
->middleware('frontend')
->namespace($this->namespace.'\Frontend')
->group(base_path('routes/frontend.php'));
}
Definition of mapAdminRoutes():
protected function mapAdminRoutes()
{
Route::prefix('admin')
->middleware('admin')
->namespace($this->namespace.'\Admin')
->group(base_path('routes/admin.php'));
}
I personally find this very useful, helps to declare interference free and logical routes. Open to feedback.
The simple way is to group both url's as separate groups. Example as follows :
Route::group(['prefix' => 'admin', 'as' => 'admin'], function () {
Route::post('/dashboard', 'AdminController#dashboard');
});
Route::group(['prefix' => 'home', 'as' => 'home'], function () {
Route::get('/record/{id}', 'HomeController#getRecord');
});

Laravel 7 - Use Middleware and except in route file

I use Laravel 7 for an API project, I have created a JWT Middleware, and I want to apply it to all my routes, except 2 of them.
For now I have in my routes/api.php :
Route::prefix('v1')->group(function () {
Route::get('ping', 'Api\Ping\PingController#ping');
// auth routes
Route::group(['prefix' => 'login/'], function () {
Route::post('login', 'Api\Auth\AuthController#login');
Route::group(['middleware' => 'jwt:api'], function() {
Route::get('me', 'Api\Auth\AuthController#me');
Route::post('refreshToken', 'Api\Auth\AuthController#refresh');
Route::post('logout', 'Api\Auth\AuthController#logout');
});
});
Route::group(['middleware' => 'jwt:api'], function() {
Route::resource('users', 'Api\User\UserController');
// my other routes protected .....
I don't like this approach because I need to copy the middleware.
I tried this approach :
Route::group(
[
'middleware' => ['jwt:api', ['except' => 'login/login']],
'prefix' => 'v1/',
], function() {
But I have this error :
Illegal offset type in isset or empty
Is it possible ? I want to group everything in my route file.
Possible solutions:
You can pass additional parameters to middleware via dots and check in middleware to do not use passed routes
Also, you can overwrite middleware and add some property\constant with array of excepts, like in csrf middleware
Implement ability in Laravel core to pass except array as in your exmaple and make a PR to framework github
Left it as you have done

How to use middleware in Laravel for secure controllers?

I need to configure middleware in Laravel that all controllers will be secured by Auth.
I mean that will redirection for every incoming request if user is not authorized.
You can do the following in your routes file:
Route::group(['prefix' => 'admin'], function () {
Route::group(['middleware' => ['auth'], function() {
...Your routes here
This will apply the auth middleware to all routes prefixed with admin. You can of course also leave the prefix away if you don't need it.

Multi auth for laravel 5.2 use middleware

I want to use multi authenticate in Laravel 5.2. But I found a strange problem.
Here is my code in route.php.
However I can not get $errors in the login.blade.php, it just returns null.
<?php
Route::group(['middleware' => ['web'], 'namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::auth();
});
?>
But when I remove the web middleware, it works.
<?php
Route::group(`enter code here`['namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::auth();
});
?>
I don't know why. My understanding is that when I want to use the session, I must use the web middleware.
Because on Laravel 5.2 the web middleware is automatically applied, your routes in routes.php are grouped with prefix App\Http\Controllers and web middleware. You can find this definition on mapWebRoutes() method of the RouteServiceProdiver.php.

Laravel5 assign Middleware to route patterns

Assume i have 1 application and two external packages.
Package 1 defines routes like api/v1/package1/foo
Package 2 defines routes like api/v1/package2/bar
In my main application i want to apply some Middleware for each api/v1/* route.
How to do this?
Use Route Groups to accomplish this. You can set prefix or domain groups, namespace groups, and middleware groups this way.
For example:
Route::group(['prefix' => 'api/v1'], function(){
Route::group(['prefix'=>'package1', 'middleware' => 'package1.middleware']), function({
Route::get('foo', 'FooController#foo');
});
Route::group(['prefix' => 'package2', 'middleware' => 'package2.middleware']), function({
Route::get('bar', 'BarController#bar');
});
});

Categories