【发布时间】:2014-12-11 22:21:52
【问题描述】:
我对 laravel 还很陌生,我正在努力让我的网址格式正确。
它的格式为http://mysite/blog?category1 而不是http://mysite/blog/category1
这些是我正在使用的文件。有没有办法把路由放入BlogController?
Route.php
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category)->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
});
博客控制器
class BlogController extends BaseController {
public function index()
{
// get the posts from the database by asking the Active Record for "all"
$posts = Post::all();
// and create a view which we return - note dot syntax to go into folder
return View::make('blog.index', array('posts' => $posts));
}
}
blog.index 刀片
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
<h2>{{ HTML::link(
action('BlogController@index',array($post->category)),
$post->category)}}
@endforeach
【问题讨论】:
-
你是用apache还是nginx,我认为这是url的重写问题。
-
“它的格式为”是什么意思?当你在浏览器中输入?还是 Laravel 生成的链接?
-
laravel 从数据库生成的链接。它现在显示为localhost/blog?category=category1,并且它也不会过滤数据库结果,所以某处有问题
标签: php url laravel laravel-4 url-routing