You can use regex to tell laravel how the "wildcard" looks https://laravel.com/docs/8.x/routing#parameters-regular-expression-constraints
Wildcard in route
Hi, I have this route in my web.php:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
It's work: name.com/firma/company-name.
I need add to to this route wildcard:
company-name.name.com
How can I make it in Laravel?
yes, but I need company-name + 'domain.com'. First login company and them my domain.
How can I write it in web.php / router?
@trifek I just tested with /firma/company-name.name.com in the url, and that works just fine as a slug. Give an actual example that doesnt work.
Do you want two wildcards in the same segment @trifek
Route::get('/firma/{company_name}.{domain}', 'CompanyController@company')->name('company');
// CompanyController
public function company($company_name, $domain)
{
//...
}
@tykus It's not working. When I have https://firma-2.domain.com - it's not working. https://domain.com/firma/firma-2 - this working fine
I have route:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::get('{company_name}.{domain}', 'CompanyController@company2')->name('company2');
and php:
public function company(string $slug)
public function company2($company_name, $domain)
@trifek that first example is called sub domain routing https://laravel.com/docs/8.x/routing#route-group-subdomain-routing
@trifek you are describing sub-domain routing - sorry, this was not obvious from your earlier messages
@tykus yes, I need subdomain
@trifek like the docs say...
Route::domain('{company}.name.com')->group(function () {
Route::get('/', function ($company) {
//
});
});
So you are looking for subdomain routing?
https://laravel.com/docs/8.x/routing#route-group-subdomain-routing
@MichalOravec yes, I try:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::domain('{slug}.domain.com')->group(function () {
Route::get('/firma/{slug}', 'CompanyController@company')->name('company2');
});
but it's not working :(
@trifek explain a bit more.
- What url are you testing with?
- What happens?
@trifek your domain is domain.com???
@tykus @sinnbeck domain.com - in web.php I have correct domain name. For this topic I change domain name :)
When I check url: https://domain.com/firma/firma-2 - it's work fine This url: https://firma-2. domain.com - show me main page
@trifek you second example with the subdomain does not have /firma/slug?
@trifek I don't know what you mean by "main page"
In any case, the route you have defined would respond to https://firma-2.domain.com/firma/firma-2 because this is the route you defined inside the sub-domain group:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company2');
@Sinnbeck firma-2 = sług :)
It may be otherwise.
I would like to have a route that:
- https://domain.com/firma/firma-2 2.https: //firma-2.domain.com
Will show me the same page :)
@trifek in that case; two routes defined which are mapped to the same Controller / action
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::domain('{slug}.domain.com')->group(function () {
Route::get('/', 'CompanyController@company')->name('company2');
});
@tykus Yes, it's true. I have 2 routes to the same Controller. It's ok.
https://domain.com/firma/firma-2 and https: //firma-2.domain.com - this it the same page :)
firma-2 - it's slug (or subdomain). domain.com - this is my main domain :)
@trifek so, your problem is solved or no?
@tykus No. ;) Again. https://domain.com/firma/firma-2 and https: //firma-2.domain.com - this it the same page :) firma-2 - it's slug (or subdomain). domain.com - this is my main domain :)
my route:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::domain('{slug}.domain.com')->group(function () {
Route::get('/', 'CompanyController@company')->name('company2');
});
Nów, when I open: https://domain.com/firma/firma-2 - I see company view. It's okey. When I open: https: //firma-2.domain.com - Iahve mainpage (main page = domain.com).
@trifek and you are using the actual domain in the route definition?
@Sinnbeck yes
@trifek How are you actually serving this application?
Is your web server configured to handle subdomains?
@tykus good question why it didn't work: /
Yes, I have application on hosting and it's configured to wildcard
Solution is:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::domain('{slug}.'. config("app.url"))->group(function () {
Route::get('/', 'CompanyController@company')->name('company2');
});
@trifek why is this different to whatever I posted earlier? config('app.url') aside; it is the same thing...
@trifek doesn't config('app.url') have https:// in the url?
So you are saying this works?
Route::domain('{slug}.https://website.com')
@Sinnbeck yes, in APP_URL=https://domain.com amazing isn't it?
@trifek APP_URL is not relevant for routing
@trifek I honestly don't see how that would ever work.. Or work better than the correct way of doing it..
@Sinnbeck How should it be correct? How should it be correct? If this current solution is not correct then I would prefer to correct it and apply the correct solution .... :)
@trifek this:
Route::get('/firma/{slug}', 'CompanyController@company')->name('company');
Route::domain('{slug}.domain.com')->group(function () {
Route::get('/', 'CompanyController@company')->name('company2');
});
Please or to participate in this conversation.