我对laravel相当新,而且我正努力让我的网址格式正确。
格式为
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 blade
@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
答案 0 :(得分:6)
routes.php文件
Route::get('category', 'CategoryController@indexExternal');
*。blade.php打印完成的URL
<a href="{{url('category/'.$category->id.'/subcategory')}}" class="btn btn-primary" >Ver más</a>
答案 1 :(得分:0)
不使用函数作为Route::get
的回调,而是使用控制器和操作:
Route::get('blog/{category}', 'BlogController@getCategory');
现在,在您的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));
}
/**
* Your new function.
*/
public function getCategory($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);
}
}
<强>更新强>
要在视图中显示您的链接,您应使用HTML::linkAction
代替HTML::link
:
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
{{ HTML::linkAction('BlogController@index', "Linkname", array('category' => $post->category)) }}
@endforeach
答案 2 :(得分:0)
您是否尝试过使用文档中显示的替代.htaccess? 你走了:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
您需要将其放在应用程序的public
文件夹中。
这是最初的.htaccess,以防你出于某种原因没有它
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
答案 3 :(得分:0)
我添加了一条新路线:
Route::get('blog/{category}', ['as' => 'post.path', 'uses' => 'BlogController@getCategory']);
并在 index.blade:
中添加了一个新链接<a href="{{ URL::route('post.path', [$post->category]) }}">{{ $post->category }}</a>