[英]How to use laravel 4 routes correctly?
I'm really new in Laravel. 我真的是Laravel新手。
Context: 内容:
But I'm a little bit confusing about routes and view. 但是我对路线和视野有些困惑。
This is my prototype: 这是我的原型:
// Posts
Route::get('add', 'PostsController@add');
Route::post('add', 'PostsController@store');
Route::resource('posts', 'PostsController', array(
'except' => array('create', 'store')
));
// Users
Route::get('login', 'UsersController@login');
Route::get('logout', 'UsersController@logout');
Route::get('profile', array(
'before' => 'auth',
'uses' => 'UserController@profile'
));
Route::get('register', UsersController@register');
Route::post('register', 'UsersController@store');
Route::resource('users', 'UsersController', array(
'except' => array('create', 'store')
));
What would you do for this simple example? 您将为这个简单的示例做什么?
I don't really know how to implement slug routing in an appropriated way. 我真的不知道如何以适当的方式实现段状路由。
Do you have good examples with appropriated routing and seo? 您是否有适当的路由和seo的好例子? (Github / BitBucket)
(Github / BitBucket)
Thanks! 谢谢!
This is an example route for post slug: 这是后的示例路线:
Route::get('posts/{slug}', array('as' => 'posts.show', 'uses' => 'PostsController@show'));
And the controller show method: 以及控制器的show方法:
class PostsController extends BaseController {
public function show($slug, $language = null)
{
if ($post = Post::findBySlug($slug))
{
return View::make('posts.article')->with('post', $post);
}
return Redirect::route('posts.index');
}
}
In your model you can add a find by slug method: 在您的模型中,您可以添加按段查找方法:
public static function findBySlug($slug)
{
return Post::where('slug', $slug)->first();
}
And to save your slug, something like: 为了节省您的,类似:
$post->title = Input::get('title');
$post->post = Input::get('text');
$post->slug = Str::slug(Input::get('title'));
$post->save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.