都说laravel优雅,通过这几天的使用我发现了无线的蛋疼,充满了反人类,不人性,难道程序员就就不能有用户体验吗,每加一个控制器就加一次路由。就先谈谈路由吧,其他的先不谈,他的优雅在哪里请大牛指点。
请举例说一下laravel的强大,不要总是说,laravel多牛,composer多好,具体好在哪里。比如从一个项目的角度出发,在具体实现某个功能,都用到了哪些功能,这些写法的好处,以及后期维护方面。
<code class="language-php"><span class="x">public function index()</span>
<span class="x"> {</span>
<span class="x"> $categories = ['新手','实例','插件','进阶'];</span>
<span class="x"> $posts = Post::with('category')->whereHas('category', function($q) use ($categories) { $q->whereIn('name', $categories); })->orderBy('created_at', 'desc')->simplePaginate(7);</span>
<span class="x"> $tags = Tag::all();</span>
<span class="x"> </span>
<span class="x"> $most_read = Post::orderBy('clicks', 'desc')</span>
<span class="x"> ->with('category')</span>
<span class="x"> ->take(5)</span>
<span class="x"> ->get();</span>
<span class="x"> </span>
<span class="x"> $feature_ids = Settings::get('features');</span>
<span class="x"> foreach ($feature_ids as $key=>$val){</span>
<span class="x"> $feature_posts[$key]=Post::findOrFail($val);</span>
<span class="x"> }</span>
<span class="x"> return view('front.index', compact('posts','tags','most_read','feature_posts'));</span>
<span class="x"> }</span>
</code>
别的不说,先说route. 不知道你用的哪个版本,Laravel 4.*? 有没有试过group, before和after filter. Laravel 5? 试试Middleware? 有没有用到RESTful? 更不要说使用Route起来SEO有多么方便。