How Can i Call Middleware in this prefix Route? - laravel

Hello I am new in laravel framework. can anyone tell me how to apply middleware in this following route?
Route::prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
});

There are various to call middleware in the group function.
1st way:- Define middleware after group function.
Route::prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
})->middleware('yourmiddlewarename');
2nd way:- to define middleware with a prefix.
Route::middleware(['yourmiddlewarename'])->prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
});

You should use Laravel's Route::group() method for proper grouping of routes.
You can group routes like the following:
Route::group(['as' => 'for_named_route','prefix' =>'for_prefixing','namespace' => 'for_namespacing', 'middleware' => 'for_middleware'],function(){
// Your route will go here
);
For your coding purpose your route group should be like the following:
Route::group(['prefix'=>'for_prefixing','middleware'=>'for_middleware'],function(){
// Your route will go here
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
);
You can also pass multiple middleware using an array like:
'middleware'=>['middleware_1','middleware_2']

Route::group(['prefix'=>'admin','middleware'=>['auth']], function(){
Route::post('favorite/{post}/add','FavoriteController#add')->name('post.favorite');
Route::post('review/{id}/add','ReviewController#review')->name('review');
Route::get('file-download/{id}', 'PostController#downloadproject')->name('project.download');
Route::post('file-download/{id}', 'PostController#downloadproject');
});

Related

Can't seem to find a way to use the route.php from 5.2 from laravel 5.2 to 8.83.25 web.php

I'm pretty new with Laravel, was able to work on finding a tutorial but it uses a
5.2 version.
I'm trying to convert the older version to 8.83.25
This is the route in the tutorial that I'm following.
I have created the CategoryController.php manually
Route::group(['middleware' => ['web']], function(){
Route::get('category', 'CategoryController');
});
What you used is wrong syntax. To pass a route to a controller, you are supposed to pass it as an array as such:
Route::group(['middleware' => ['web']], function(){
Route::get('category', [CategoryController::class]);
});
Now supposing you want to pass it to a particular function(lets say the function store) in your controller, you just indicate that in the array as such:
Route::group(['middleware' => ['web']], function(){
Route::get('category', [CategoryController::class, 'store']);
});
Take a look at the docs to learn more about laravel v8 routing.

Laravel use multiple subdomains in one app

My question can we use something like this in laravel routing like one routes for admin.domain.com and other routes for clients.domain.com. How achieve this in laravel routing.
For example:
// these needs to go clients.domain.com/route
Route::get('/clients', 'App\Http\Controllers\HomeController#index')->name('clients');
Route::get('/clients/order', 'App\Http\Controllers\KuponaiController#pridetiKupona')->name('clients.order');
//this one to admin.domain.com/admin
Route::get('/admin', 'App\Http\Controllers\KuponaiController#patvirtinimokodas')->name('dashboard');
There is clear documentation on how you can use subdomains in your Laravel application here https://laravel.com/docs/8.x/routing#route-group-subdomain-routing.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
For this to work, you need to have this subdomain before registering root domain routes
Create a route group and pass an array with a domain property:
Route::group(['domain' => 'clients.domain.com'], function()
{
// clients.domain.com/clients
Route::get('/clients', 'App\Http\Controllers\HomeController#index')->name('clients');
// clients.domain.com/clients/order
Route::get('/clients/order', 'App\Http\Controllers\KuponaiController#pridetiKupona')->name('clients.order');
});
Route::group(['domain' => 'admin.domain.com'], function()
{
// admin.domain.com/admin
Route::get('/admin', 'App\Http\Controllers\KuponaiController#patvirtinimokodas')->name('dashboard');
});

Use different route namespace based on middleware in Laravel

I have the following code in my routes/web.php
Route::namespace('Admin')->middleware(['admin'])->group(function() {
Route::get('/posts', 'PostController#index');
});
Route::namespace('User')->middleware(['user'])->group(function() {
Route::get('/posts', 'PostController#index');
});
I wish to use the same uri "/posts" in both cases and keep the role logic (admin, user) out of the controllers, however, in this case, when I request the route "/posts" in always responds with the last one.
I can't seem to find information of what I am missing here.
use prefix for different route for admin and user
/admin/posts
Route::group(['namespace' => 'Admin','middleware=>'admin','prefix' => 'admin'],function() {
Route::get('/posts', 'PostController#index');
});
/user/posts
Route::group(['namespace' => 'User','middleware=>'user','prefix' => 'user'],function() {
Route::get('/posts', 'PostController#index');
});
You may try this one
Route::group(['prefix'=>'admin','middleware'=>'admin'],function (){
Route::get('/posts',['uses'=>' PostController#posts','as'=>'posts.index']);
});
Route::group(['prefix'=>'user','middleware'=>'user'],function (){
Route::get('/index',['uses'=>' PostController#posts','as'=>'posts.index']);
});

How to add dynamically prefix to routes?

In session i set default language code for example de. And now i want that in link i have something like this: www.something.com/de/something.
Problem is that i cant access session in routes. Any suggestion how can i do this?
$langs = Languages::getLangCode();
if (in_array($lang, $langs)) {
Session::put('locale', $lang);
return redirect::back();
}
return;
Route::get('blog/articles', 'StandardUser\UserBlogController#AllArticles');
So i need to pass to route as prefix this locale session.
If you want to generate a link to your routes with the code of the current language, then you need to create routes group with a dynamic prefix like this:
Example in Laravel 5.7:
Route::prefix(app()->getLocale())->group(function () {
Route::get('/', function () {
return route('index');
})->name('index');
Route::get('/post/{id}', function ($id) {
return route('post', ['id' => $id]);
})->name('post');
});
When you use named routes, URLs to route with current language code will be automatically generated.
Example links:
http://website.com/en/
http://website.com/en/post/16
Note: Instead of laravel app()->getLocale() method you can use your own Languages::getLangCode() method.
If you have more questions about this topic then let me know about it.
Maybe
Route::group([
'prefix' => Languages::getLangCode()
], function () {
Route::get('/', ['as' => 'main', 'uses' => 'IndexController#index']);
});

Laravel 4 : Default route for route group

I try to handle the default route of a route group, I have this but it doesn't work.
Route::group(array('prefix' => 'administrator'), function() {
Route::get('/', 'AdminUserController#getLogin');
Route::controller('page', 'AdminPageController');
Route::controller('user', 'AdminUserController');
Route::controller('menu', 'AdminMenuController');
});
Does anyone know how to do that ?
Thank you
Just figured out. You are missing a uses, and the second parameter should be an array.
Route::get('/', ['uses' => 'AdminUserController#getLogin']);

Resources