Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Capture domain in Laravel routes file as url variable

I have a problem capturing the domain in the routes file.

When a route is written like this:

Route::domain('mydomain.com')
    ->get('/', function () {
        return "It works";
    });

And I access mydomain.com, I correctly get status 200 and a string It works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But when the routes are changed to this:

Route::domain('{domain}')
    ->get('/', function () {
        return "It works";
    });

It is not getting picked up and I get 404.

I was also trying:

Route::bind('domain', function () {
    return 'mydomain.com';
});

Route::domain('{domain}')
    ->get('/', function () {
        return "It works";
    });

But it is not working either.

Any idea what am I doing wrong?

>Solution :

Define a custom pattern for the {domain} parameter using regular expressions.

Route::pattern('domain', '[a-z0-9.-]+'); // Define pattern for the domain parameter

Route::domain('{domain}')
    ->get('/', function ($domain) {
        return "It works for {$domain}";
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading