我对laravel相当陌生,而且我正努力使我的url格式正确。在laravel中通过url传递变量
它格式,
http://mysite/blog?category1 instead of 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);
});
Blogcontroller
方式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('[email protected]',array($post->category)),
$post->category)}}
@endforeach
你对Apache或nginx的,我觉得这是URL重写问题。 – 2014-10-16 14:00:06
“它的格式为”是什么意思?当你输入浏览器时?或者Laravel生成的链接? – 2014-10-16 14:32:36
laravel从db生成的链接。它现在显示为http:// localhost/blog?category = category1,它也不会过滤数据库结果,所以某处出错了。 – 2014-10-16 15:01:12