如何使用 laravel 在一个 url 中显示两个动态名称
How to display two dynamic names within one url using laravel
以下代码:
{{url('/'.$subjecttype->name)}}
是名字 'garden' 包裹在 url 中。这给了我 localhost/garden 显然 garden 作为动态名称。我的路线设置如下:
Route::get('/{subject}/', array( 'as' => 'subject', 'uses' =>
'SubjectController@getsubject'));
问题是如何在一个路由中设置两个动态名称? 例如
localhost/garden/12
所以我希望我的路线看起来像这样
Route::get('/{subject}/{id}/', array( 'as' => 'subjectid', 'uses' => 'SubjectController@getsubjectid'));
但更重要的是,在我看来它会是什么样子? 这样我就可以将我的花园 header 包裹在一个 url 中看起来像这样:
'gardening tips for beginners' 即 {{$subjecttype->title}}
下面是我对我想要的东西的非常糟糕的尝试,但我希望你能明白。
{{url('/$subjecttype->name/$subjecttype->id/'.$subjecttype->title)}}
谢谢
您的路线:
Route::get(
'/{subject}/{id}/',
array(
'as' => 'subjectid',
'uses' => 'SubjectController@getsubjectid'
)
);
您可以使用以下代码生成 URL:
$url = URL::route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
或者,如果您更喜欢使用辅助函数:
$url = route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
这会给你一个 URL 就像 http://example.com/subjectname/42
。如果您想在 URL 末尾添加另一个参数,例如标题,您需要将另一个参数添加到路由定义中。如果你不这样做,你会得到 404 错误,因为没有路由会匹配 URL.
我的问题的第二部分使用 'gardening tips for beginners' 示例:
<a href="{{ URL::to(action('SubjectController@getsubjectid', array($subjecttype->title, $subjecttype->id)))}}">gardening tips for beginners</a>
以下代码:
{{url('/'.$subjecttype->name)}}
是名字 'garden' 包裹在 url 中。这给了我 localhost/garden 显然 garden 作为动态名称。我的路线设置如下:
Route::get('/{subject}/', array( 'as' => 'subject', 'uses' =>
'SubjectController@getsubject'));
问题是如何在一个路由中设置两个动态名称? 例如
localhost/garden/12
所以我希望我的路线看起来像这样
Route::get('/{subject}/{id}/', array( 'as' => 'subjectid', 'uses' => 'SubjectController@getsubjectid'));
但更重要的是,在我看来它会是什么样子? 这样我就可以将我的花园 header 包裹在一个 url 中看起来像这样:
'gardening tips for beginners' 即 {{$subjecttype->title}}
下面是我对我想要的东西的非常糟糕的尝试,但我希望你能明白。
{{url('/$subjecttype->name/$subjecttype->id/'.$subjecttype->title)}}
谢谢
您的路线:
Route::get(
'/{subject}/{id}/',
array(
'as' => 'subjectid',
'uses' => 'SubjectController@getsubjectid'
)
);
您可以使用以下代码生成 URL:
$url = URL::route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
或者,如果您更喜欢使用辅助函数:
$url = route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
这会给你一个 URL 就像 http://example.com/subjectname/42
。如果您想在 URL 末尾添加另一个参数,例如标题,您需要将另一个参数添加到路由定义中。如果你不这样做,你会得到 404 错误,因为没有路由会匹配 URL.
我的问题的第二部分使用 'gardening tips for beginners' 示例:
<a href="{{ URL::to(action('SubjectController@getsubjectid', array($subjecttype->title, $subjecttype->id)))}}">gardening tips for beginners</a>