Laravel group routing with parameter - laravel-5

I have this group in my route:
}
Route::group(['prefix' => 'v1/en'], function() {}
is it possible to change the 'en' segment uri in a parameter, so it can be changed depending on the request? Something like this:
Route::group(['prefix' => 'v1/{lang}'], function() {}

Yes you can, $lang will be available if you define your route group like this:
Route::group(['prefix' => 'v1/{lang}'], function() {
// ...
});
or with new syntax:
Route::prefix('v1/{lang}')->group(function() {
// ...
});
You can access it anywhere with:
request()->route()->parameter('lang');
You might want to checkout packages like this: Localization for Laravel
They basically do the same, and have nice middleware implementations to set locales etc.

How about to generate dynamically another route group with prefix $lang inside of group with v1 prefix?
$langs = ['en', 'de', 'it'];
Route::group(['prefix' => 'v1'], function() use ($langs) {
foreach($langs AS $lang) :
Route::group(['prefix' => $lang], function() {
Route::get('something', 'SomethingController#list');
});
endforeach;
});
or same logic (taken from here):
Route::group(['prefix' => 'v1'], function() use ($langs) {
Route::group(['prefix' => '{lang}'], function() {
Route::get('something', 'SomethingController#list');
});
});

Related

simulate route in laravel

I have this route defined
Route::get('test/something/{id?}', 'MyController#mymethod')->name('test.something');
So if go to domain_1.com/test/something/123 I get some page with data.
Now, I want to to show the exact same thing if the site if accessed form another domain with subdomain. I've defined this:
Route::group(['domain' => 'subdomain.domain_2.com'], function() {
Route::get('/', 'MyController#mymethod', ['id' => 123]);
});
but I got to subdomain.domain_2.com, I get Too few arguments error. How can I pass the id parameter to the method in the route?
You could use redirect like:
Route::get('test/something/{id?}', 'MyController#mymethod')->name('test.something');
Route::group(['domain' => 'subdomain.domain_2.com'], function() {
Route::get('/', function(){
return redirect('/test/something/123');
});
});
If you don't want to use redirect, you could check the current url from the method in the controller like:
public function mymethod($id=null){
$currentUrl = request()->url();
if (strpos($currentUrl, 'subdomain') !== false) {
$id = 123;
}
// your function
}
I've found this piece of code that gets the job done, but I'm not sure is the correct way to achieve what I want
Route::group(['domain' => 'subdomain.domain_2.com'], function()
{
Route::get('/', [ 'as' => 'some_alias', function()
{
return app()->make(App\Http\Controllers\MyController::class)->callAction('mymethod', $parameters = [ 'id' => 123 ]);
}]);
});

How can I add one route to 2 different middleware (auth) without having to duplicate it in Laravel?

I know this is a basic laravel question but don't know how do it. How can I add one route to 2 different middleware (auth) without having to duplicate it?
// =admin
Route::group(['middleware' => ['auth']], function() {
Route::get('/dashboard', 'App\Http\Controllers\DashboardController#index')->name('dashboard');
Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
});
// =cashier
Route::group(['middleware' => ['auth', 'role:cashier']], function() {
Route::get('/dashboard/cashier/profile', 'App\Http\Controllers\DashboardController#showCashierProfile')->name('dashboard.cashier.profile');
Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
});
I have this route and I don't want to repeat calling this per auth middleware: Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
You can't have two routes with the same url.
Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
This route is inside both groups and since the url they will produce will be the same, only the second will remain.
Route::group(['middleware' => ['auth']], function() {
Route::get('/dashboard', 'App\Http\Controllers\DashboardController#index')->name('dashboard');
//Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
// this route will be ignored because the other one has the same url
});
Route::group(['middleware' => ['auth', 'role:cashier']], function() {
Route::get('/dashboard/cashier/profile', 'App\Http\Controllers\DashboardController#showCashierProfile')->name('dashboard.cashier.profile');
Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
});
If you want Laravel to handle these two routes differently, you have to add a prefix:
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['auth']], function() {
Route::get('/dashboard', 'App\Http\Controllers\DashboardController#index')->name('dashboard');
//Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
// this route will be ignored because the other one has the same url
});
Route::group(['prefix' => 'cashier', 'as' => 'cashier.', 'middleware' => ['auth', 'role:cashier']], function() {
Route::get('/dashboard/cashier/profile', 'App\Http\Controllers\DashboardController#showCashierProfile')->name('dashboard.cashier.profile');
Route::get('make-a-sale', [PurchasesController::class, 'index'])->name('make-a-sale.index');
});
This way, when the url will be prefixed with admin, the first route will be called (without the role:cashier middleware).
Notice that I added a route name prefix ('as' => 'admin.' / 'as' => 'cashier.') so you can call each one by name, using:
route('admin.make-a-sale.index'); // admin/make-a-sale
//or
route('cashier.make-a-sale.index'); // cashier/make-a-sale
Just to add, if someone wants to fix the Laravel blade error below whenever you clear your browser cache and was automatically logout:
*Attempt to read property "name" ...*
You need to add all your routes inside the:
Route::group(['middleware' => ['auth']], function () {
// routes here
});
This will redirect you to login once that happens.

Multi domain routing in Laravel 5.2

I have setup multi-domain routing in my laravel 5.2 app. What I want to achieve is if a user hits, membership.app, he should be served different homepage as compared to user who hits, erp.app domain.
Route::pattern('erp', 'erp.app|erp.domain.com');
Route::pattern('membership', 'membership.app|membership.domain.com');
Route::group(['middleware' => ['web', 'auth'], 'domain' => '{erp}'], function() {
Route::get('/', 'HomeController#getIndex');
Route::controller('members', 'MembersController');
Route::controller('users', 'UsersController');
Route::controller('settings', 'SettingsController');
});
Route::group(['middleware' => 'web', 'domain' => '{erp}'], function () {
Route::controller('auth', 'Auth\AuthController');
});
Route::group(['middleware' => 'web', 'domain' => '{membership}'], function () {
Route::controller('/', 'BecomeMemberController');
});
Route::group(['middleware' => 'web'], function () {
Route::controller('ajax', 'AjaxController');
});
I tried this setup, but it breaks the code with first param in each controller method being the url instead of intended value.
Suppose I have a method hello in members controller.
public function hello($param1, $param2)
{
....
}
If I access erp.app/members/hello/1/2 url and try to print out $param1 of controller method, it returns erp.app instead of intended 1 in this case.
Please help.
I don't know why aren't you seperating the routes to different controllers as you say the output will be quite different...
A quick example of to use that:
Route::group(['domain' => '{type}.myapp.com'], function () {
Route::get('members/hello/{id1}/{id2}', function ($type, $id1, $id2) {
// when you enter --> members.myapp.com/hello/12/45
var_dump($type); //memebers
var_dump($id1); //12
var_dump($id2); //45
});
});

route group prefixed for admin

How do you have a the start of the route to have admin at the begining of the route like '/admin/attributes/1/edit in the routes group collection instead of just /attributes/1/edit
Route::group(array('before' => 'Admin'), function() {
Route::resource('attributes', 'AttributesController');
Route::resource('brands', 'BrandsController');
Route::resource('products', 'ProductsController');
Route::resource('tags', 'TagsController');
Route::resource('roles', 'RolesController');
Route::resource('suppliers', 'SuppliersController');
});
You need to use a prefix
Route::group(array('prefix' => 'admin'), function()
{
// routes here
});
See Laravel Documentation

Laravel - routing and language code

I need to put a language code in my url but not when it is the default one.
Here is the code in routes.php file in Laravel 4.2
I need root structure like:
default language => http://website.com/RegistrationStep1
other language => http://website.com/language/RegistrationStep1
Route::group(['prefix' => '{lang?}', 'before' => 'localization'], function()
{
Route::get('/', function() {
return View::make('hello');
});
Route::get('registration/step1', 'RegistrationController#getRegistrationStep1');
Route::post('registration/step1', 'RegistrationController#postRegistrationStep1');
});
I am getting error when I call url without the language param in url
First, define your available languages:
# in app/config/app.php
'available_locales' => array('de', 'en', 'es', 'fr', 'it'),
In your routes.php check if the first segment of current URI is a valid language shortcut before register your prefix in route group.
$locale = Request::segment(1);
if (in_array($locale, Config::get('app.available_locales'))) {
\App::setLocale($locale);
} else {
$locale = null;
}
Route::group(array('prefix' => $locale), function()
{
//your routes here
});
See the link http://learninglaravel.net/language-switching-in-laravel-4/link
You can also use this package for your task: mcamara/laravel-localization

Resources