美文网首页
view()->exists() 方法的使用

view()->exists() 方法的使用

作者: 晨曦入诗 | 来源:发表于2018-10-07 23:02 被阅读2次

    Laravel News 网站的 Laravel Tutorials 和 Laravel Packages 板块其实是文章的两个分类,你看它们的请求地址就知道了。

    class CategoryController extends Controller
    {
            public function show($slug)
             {
                    $category = Category::with('posts')->where('slug', $slug)->firstOrFail();
                    if ( view()->exists('catogory.custom.' . $category->slug)){
                             $view = 'category.custom.' . $category->slug;
                    } else {
                            $view = 'category.show';
                    }
                    return view( $view, [
                          'category' => $category,
                          'posts' => $category->posts()->orderBy('published_at', 'desc')->paginate(10);
                    ]);
             }
    }
    

    view()->exists 方法是用来判断指定视图文件是否存在,存在返回 true ,不存在返回 false 。在上面的代码实例中,Laravel Tutorials 使用到了自定义文件 laravel-tutorials.blade.php ,而 Laravel Packages 没有自定义布局文件,使用的则是默认布局文件 show.blade.php

    相关文章

      网友评论

          本文标题:view()->exists() 方法的使用

          本文链接:https://www.haomeiwen.com/subject/lvbraftx.html