我對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