在Laravel路由文件中捕获域名作为URL变量。

huangapple go评论59阅读模式
英文:

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.

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?

英文:

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.

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?

答案1

得分: 0

定义一个使用正则表达式的自定义模式,用于 {domain} 参数。

Route::pattern('domain', '[a-z0-9.-]+'); // 为 domain 参数定义模式

Route::domain('{domain}')
    ->get('/', function ($domain) {
        return "它适用于 {$domain}";
    });
英文:

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

huangapple
  • 本文由 发表于 2023年6月5日 03:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定