Laravel 4 中的管理路由(或前缀路由)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14463344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Admin Routes (or Prefix Routes) in Laravel 4
提问by Patrick Maciel
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
如何在 Laravel 4(Restfull 控制器)中创建特定于管理员的路由:
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
我想知道:
- What Files and where I need create theam
- How I need create the route
- 什么文件以及我需要在哪里创建它们
- 我需要如何创建路线
回答by David Fregoli
In Laravel 4
you can now use prefix
:
在Laravel 4
您现在可以使用prefix
:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController@home');
Route::get('posts', 'AdminController@showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
回答by Pierre Broucz
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
对于您的子文件夹,正如我在此回答的"route-to-controller-in-subfolder-not-working-in-laravel-4",在这个 laravel 4 beta 中似乎没有“友好”的解决方案。
@Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
@Aran,如果你让它工作正常,请添加你的控制器、路由和 composer.json 文件的代码示例:
Route::resource('admin/users', 'admin.Users');
or
或者
Route::resource('admin', 'admin.Users');
thanks
谢谢
回答by Aran
Really useful tool that you can use is the artisan
CLI.
您可以使用的真正有用的工具是artisan
CLI。
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
使用它,您将能够生成所需的函数文件,其中包含使其成为 RESTful 所需的所有路由。
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
将为您生成函数文件。然后在你的 routes.php 文件中你可以简单地添加
Route::resource('users', 'Users');
This'll setup all the necessary routes.
这将设置所有必要的路由。
For more information on this you should read the documentation at.
有关这方面的更多信息,您应该阅读文档。
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
http://four.laravel.com/docs/artisan
Edit:
编辑:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
为了使这个管理员特定,简单地改变如下代码并将控制器移动到控制器文件夹内的管理文件夹。
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
第一个参数是路由,第二个是控制器文件名/文件夹。
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
在 Laravel 中,如果您将控制器放置在文件夹中,要在路由或 URL 中指定它,您将使用 a 点作为文件夹。
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
然后,您可以对此进行扩展并使用路由过滤器添加身份验证,特别是在下面的页面上找到的“基于模式的过滤器”的代码。
回答by Daniel Lee
Laravel 4 - Add Admin Controller Easily
Laravel 4 - 轻松添加管理控制器
This was driving me insane for ages, but i worked it out.
这让我疯狂了很多年,但我解决了。
routes.php
路由文件
Route::resource('admin', 'Admin_HomeController@showIndex');
/controllers/Admin/HomeController.php
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
注意文件夹名称 Admin 必须是大写的“A”
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
或者,您可以使用 group 方法
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController@showIndex');
});
Thanks Daniel
谢谢丹尼尔