英文:
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}";
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论