dongxingji3882 2014-10-19 12:08
浏览 58
已采纳

定义多个变量laravel以在if语句中使用

I would like to be able to display different content depending on which variable is called in the url.Ignoring the fact $slug is slug, let's say it's a post id instead so if $id is active then show just the post else if $month is active show all posts from that month elseif $month and $id = null show all. That's what I'm trying to achive

Routes.php

Route::get('blog/{slug}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']);
Route::get('blog/{month}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']); 

BlogController.php

<?php

class BlogController extends 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
  • 写回答

1条回答 默认 最新

  • dongzhijing8202 2014-10-19 16:09
    关注

    You really can't do that, because you just declared the same route twice. When you say a route is blog/{slug}, slug is only a placeholder. It's exactly the same¹ as blog/{month}. All it says is: "I expect blog/ followed by anything." It doesn't matter if you call your anything slug or month. That is, the parameters do not add up.

    What you can do is, if you consider slug is always a string and month is always a number (or the name of the month²), is to apply where clauses to your route parameters, like so:

    // Route for all blog posts
    Route::get('blog', 'BlogController@showAll');
    
    // Route for all blog posts in a month; only numbers as parameters
    Route::get('blog/{month}', 'BlogController@showByMonth')
         ->where('month', '[0-9]+');
    
    // Route for all blog posts by title; anything else as parameters
    Route::get('blog/{slang}', 'BlogController@showBySlang');
    

    And on your controller, define three methods, one for each route:

    public function showAll() {
        $blogs = Blog::all();
    
        return View::make('pages.blog')
                   ->with('blogs', $blogs);
    }
    
    public function showByMonth($month) {
        $blogs = Blog::where('month', $month)
                     ->get();
    
        return View::make('pages.blog')
                   ->with('blogs', $blogs);
    }
    
    public function showBySlug($slug) {
        $blogs = Blog::where('slug', $slug)
                     ->get();
    
        return View::make('pages.blog')
                   ->with('blogs', $blogs);
    }
    

    ¹ Unless you're using some more advanced routing features, but that's not really the point here.

    ² It is doable with the where clause, but it would be ugly if you wanted to consider all upper/lower case combinations. E.g: ([Jj][Aa][Nn][Uu][Aa][Rr][Yy]|[Ff][Ee][Bb][Rr][Uu][Aa][Rr][Yy]|...)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题