【发布时间】:2014-10-08 19:14:28
【问题描述】:
我最近开始学习 Laravel。在这样做的过程中,我开始完成网络上的教程,包括来自 youtube。在一个这样的教程中,我一直在开发一个非常基本的博客。在使用蛞蝓时,我遇到了一些僵局。出于某种原因,它只是不想为我工作!
当在我的“routes.php”文件中输入以下代码时,我注意到大括号“{”和“}”会自动转换为 URL 编码并显示“slug”一词。此外,posts 后面的 '/' 无处可见。这意味着链接将读作“http:[域名]/posts/%7Bslug%7D[文章名称]”
Route::get('/posts/{slug}', [
'as' => 'post-show',
'uses' => 'PostController@getIndex'
]);
如果我这样更改代码,问题将部分解决:
Route::get('/posts/{slug?}', [
'as' => 'post-show',
'uses' => 'PostController@getIndex'
]);
“slug”一词不再显示为 URL 的一部分。然而,由于某种原因,蛞蝓前面的“/”仍然不可见,就好像被有问题的蛞蝓吃掉了一样!现在显示为“http:[域名]/posts**%7Bslug%7D**[文章名称]”。
如果有人能伸出援手,我非常感谢您指出正确的方向。谢谢。
【问题讨论】:
-
欢迎来到 SO。您如何将这些链接插入到您的视图中?一种方法是
{{ route('post-show', ['slug' => '123']) }}。 Other ways are documented online. -
感谢主教的快速回复。我使用这种方法将链接插入到我的视图中:
@if($posts->count()) @foreach($posts as $post) <article> <h2><a href="{{ URL::action('post-show'), $post->slug }}">{{ $post->title }}</a></h2> {{ Markdown::parse(Str::limit($post->body, 157)) } <a href="{{ URL::action('post-show'), $post->slug }}">read more&hellip;</a> </article> @endforeach @endif