Laravel 4:路由到 localhost/controller/action
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18178023/
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
Laravel 4 : Route to localhost/controller/action
提问by samuraiseoul
I'm more or less new to Laravel 4. I've never used routes before but normally what I'm used to is url/controller/action and then the backend routing for me. I've read the documentation for routes and controllers a few times as well as read through some tutorials and so, I'm trying to figure out how to get this to work without writing a route for every controller and action.
我或多或少是 Laravel 4 的新手。我以前从未使用过路由,但通常我习惯的是 url/controller/action,然后是我的后端路由。我已经阅读了几次路由和控制器的文档并阅读了一些教程,因此,我试图弄清楚如何在不为每个控制器和操作编写路由的情况下使其工作。
I tried something like
我试过类似的东西
Route::get('{controller}/{action}', function($controller, $action = 'index'){
return $controller."@".$action;
});
Now then, I know this is wrong since it doesn't work, but what am I missing? On most tutorials and stuff I'm seeing an route for more or less every controller and action like:
现在,我知道这是错误的,因为它不起作用,但是我错过了什么?在大多数教程和内容中,我看到或多或少每个控制器和动作的路线,例如:
Route::get('/controller/action' , 'ControllerName@Action');
Which seems silly and like a waste of time to me.
这对我来说似乎很愚蠢,而且是在浪费时间。
Is there anyway to achieve what I want?
有没有办法实现我想要的?
回答by Makita
If you are looking for a more automated routing, this would be the Laravel 4 way:
如果您正在寻找更自动化的路由,这将是 Laravel 4 的方式:
Route:
路线:
Route::controller('users', 'UsersController');
Controller (in this case UsersController.php):
控制器(在本例中为 UsersController.php):
public function getIndex()
{
// routed from GET request to /users
}
public function getProfile()
{
// routed from GET request to /users/profile
}
public function postProfile()
{
// routed from POST request to /users/profile
}
public function getPosts($id)
{
// routed from GET request to: /users/posts/42
}
As The Shift Exchange mentioned, there are some benefits to doing it the verbose way. In addition to the excellent article he linked, you can create a name for each route, for example:
正如 The Shift Exchange 所提到的,以冗长的方式进行操作有一些好处。除了他链接的优秀文章,你还可以为每个路由创建一个名称,例如:
Route::get("users", array(
"as"=>"dashboard",
"uses"=>"UsersController@getIndex"
));
Then when creating urls in your application, use a helper to generate a link to a named route:
然后在您的应用程序中创建 url 时,使用帮助程序生成指向命名路由的链接:
$url = URL::route('dashboard');
Links are then future proofed from changes to controllers/actions.
然后,链接可以从控制器/动作的更改中得到验证。
You can also generate links directly to actions which would still work with automatic routing.
您还可以直接生成仍可用于自动路由的操作的链接。
$url = URL::action('UsersController@getIndex');
回答by Alex
app\ controllers\ Admin\ AdminController.php IndexController.php
Route::get('/admin/{controller?}/{action?}', function($controller='Index', $action='index'){
$controller = ucfirst($controller);
$action = $action . 'Action';
return App::make("Admin\{$controller}Controller")->$action();
});
Route::get('/{controller?}/{action?}', function($controller='Index', $action='index'){
$controller = ucfirst($controller);
$action = $action . 'Action';
return App::make("{$controller}Controller")->$action();
});
回答by allencoded
I come from .Net world and routing is typically done:
我来自 .Net 世界,路由通常完成:
/{Controller}/{action}/{id}
Which looks like:
看起来像:
/Products/Show/1 OR /Products/Show/Beverages
In Laravel I accomplish this routing like so:
在 Laravel 中,我像这样完成这个路由:
Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='index', $id = null) {
$controller = ucfirst($controller);
return APP::make("{$controller}Controller")->$action($id);
});
The controller would look roughly like so:
控制器大致如下所示:
class ProductsController extends BaseController {
public function Show($id) {
$products = array( 1 => array("Price" => "0","Item" => "iPhone 6"),
2 => array("Price" => "0", "Item" => "iPhone 6 Plus") );
if ($id == null) {
echo $products[1]["Item"];
} else {
echo $products[$id]["Item"];
}
}
}