Laravel pass subdomain as a value to another route - laravel

now I have this code
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/', function($subdomain)
{
Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index');
});
});
I want to pass {subdomain} to Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index'); for when call subdomain.localhost/identifier I call AController#index I should pass the value of subdomain to AController#index

I think by using Route::input('subdomain'); you can access the subdomain parameter.
Inside AController:
public function index() {
dd(Route::input('subdomain'));
}

Please try this.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});

I figured out the answer thanks guys for your help ,
the answer is just call the route inside route::group
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/{identifier}/{reCreate?}','AController#index');
});
and inside Controller function index I can access the value as normal function($subdomain)

Related

Laravel route group with variable prefix and where condition

I would like to create a route group in Laravel with a variable as a prefix. I need to set certain conditions too. How to do it properly?
I was following docs: https://laravel.com/docs/8.x/routing#route-group-prefixes but there are only general examples.
This code should create 2 routes: /{hl}/test-1 and /{hl}/test-2 where {hl} is limited to (en|pl), but it gives an error: "Call to a member function where() on null"
Route::prefix('/{hl}')->group(function ($hl) {
Route::get('/test-1', function () {
return 'OK-1';
});
Route::get('/test-2', function () {
return 'OK-2';
});
})->where('hl','(en|pl)');
The group call doesn't return anything so there is nothing to chain onto. If you make the where call before the call to group, similarly to how you are calling prefix, it will build up these attributes then when you call group it will cascade this onto the routes in the group:
Route::prefix('{hl}')->where(['h1' => '(en|pl)'])->group(function () {
Route::get('test-1', function () {
return 'OK-1';
});
Route::get('test-2', function () {
return 'OK-2';
});
});
By analogy with this answer:
Route::group([
'prefix' => '{hl}',
'where' => ['hl' => '(en|pl)']
], function ($hl) {
Route::get('/test-1', function () {
return 'OK-1';
});
Route::get('/test-2', function () {
return 'OK-2';
});
});
Does this solve your problem?

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

Laravel 5 - Named route with parameter

In my web.php file, I created two routes :
Route::get('/{name}', 'PublicController#index')->name('welcome');
Route::get('stats', function () { return route('welcome', 'enrique'); });
My controller looks like:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PublicController extends Controller
{
public function index($name)
{
return view('welcome');
}
}
I already set up a virtual host in my local machine which is http://blog.test
When I Call http://blog.test/stats in my browser, it shows me the content of my homepage. But when I reorganize my twho route in web.php file in that way
Route::get('stats', function () { return route('welcome', 'enrique'); });
Route::get('/{name}', 'PublicController#index')->name('welcome');
It works fine.
Can you please explain why it behaves like that? Thanks
What you have is the same route being overwritten. In order to have them both work, you will have to add something before your custom parameter:
/something/{name}
Otherwise stats is assumed to be value for your parameter name
Laravel route goes to first matched routes, so in your case if it sees /stats
Route::get('/{name}', 'PublicController#index')->name('welcome');
It becomes variable $name for PublicController#index
Check more article regarding this
It is happening because , when you add your {parameter} as after / , all routes defined after that, are considered as of that type
Route::get('/{name}', 'PublicController#index')->name('welcome');
// below routes not work
Route::get('stats', function () {});
Route::get('test', function () { });
Route::get('hello', function () {});
same thing happen if you create new route like below :
Route::get('post/{slug}', function () {});
// this get routes are also not work
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
so it is good practice that always define parameterised route at the
last
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
Route::get('post/{slug}', function () {});

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

how to use the laravel subdomain routing function

I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

Resources