【发布时间】:2014-10-20 01:38:36
【问题描述】:
我希望能够根据 url 中调用的变量显示不同的内容。忽略 $slug 是 slug 的事实,假设它是一个帖子 ID,所以如果 $id 处于活动状态,那么只显示帖子如果 $month 处于活动状态,则显示该月的所有帖子 elseif $month 和 $id = null 显示所有。这就是我想要达到的目标
Routes.php
Route::get('blog/{slug}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']);
Route::get('blog/{month}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']);
博客控制器.php
<?php
类 BlogController 扩展 BaseController {
public function BlogIndex()
{
//get the posts from the database by asking the Active Record for "all"
$blogs = Blog::all();
$blogs = DB::table('blogs')->paginate(3);
// and create a view which we return - note dot syntax to go into folder
return View::make('pages.blog', array('blogs' => $blogs));
}
public function ShowbySlug($slug)
{
$blogs = Blog::where('slug', '=', $slug)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('slug', $slug)
->with('blogs', $blogs);
}
public function ShowbyMonth($month)
{
$blogs = Blog::where('month', '=', $month)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('month', $month)
->with('blogs', $blogs);
}
}
blog.blade.php
@foreach ($blogs as $blog)
@if(isset($blogs))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
<a href="{{ URL::route('blog.slug', [$blog->slug]) }}">
</div>
@elseif(isset($slug))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
</div>
@endif
@endforeach
【问题讨论】:
-
使用
empty或isset代替is_null。 -
似乎没有根据哪个变量处于活动状态而改变内容,我错过了什么吗?
-
那是因为
blogs总是被设置 -
有没有办法取消设置?
-
你正在使用它,所以没有。只需
@if(empty($slug)) .. @else .. @end。
标签: php url if-statement laravel laravel-4