【问题标题】:Laravel 4 : Route to localhost/controller/actionLaravel 4:路由到 localhost/controller/action
【发布时间】:2013-08-13 05:38:45
【问题描述】:

我或多或少是 Laravel 4 的新手。我以前从未使用过路由,但通常我习惯的是 url/controller/action,然后是我的后端路由。我已经阅读了几次路由和控制器的文档,并通读了一些教程,因此,我试图弄清楚如何在不为每个控制器和动作编写路由的情况下使其工作。

我尝试了类似的东西

Route::get('{controller}/{action}', function($controller, $action = 'index'){
    return $controller."@".$action;
});

那么,我知道这是错误的,因为它不起作用,但我错过了什么?在大多数教程和内容中,我或多或少地看到了每个控制器和操作的路线,例如:

Route::get('/controller/action' , 'ControllerName@Action');

这对我来说似乎很愚蠢而且浪费时间。

有没有办法实现我想要的?

【问题讨论】:

标签: laravel laravel-4 url-routing laravel-routing


【解决方案1】:

如果您正在寻找更自动化的路由,这将是 Laravel 4 方式:

路线:

Route::controller('users', 'UsersController');

控制器(在本例中为 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
}

正如 The Shift Exchange 所提到的,以冗长的方式进行操作有一些好处。除了他链接的优秀文章,你可以创建一个name for each route,例如:

Route::get("users", array(
    "as"=>"dashboard",
    "uses"=>"UsersController@getIndex"
));

然后在您的应用程序中创建 url 时,使用帮助器生成 link to a named route

$url = URL::route('dashboard');

然后,链接可以防止控制器/操作发生变化。

您还可以直接生成指向仍可用于自动路由的操作的链接。

$url = URL::action('UsersController@getIndex');

【讨论】:

  • 如果函数有多个单词,比如getOldProfile()。这将是对用户/旧配置文件的 GET 请求
【解决方案2】:
应用程序\
    控制器\
        行政\
           管理员控制器.php
        索引控制器.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();
    });

【讨论】:

  • 请解释为什么这个答案有帮助。
  • 这是自动为所有控制器路由控制器/动作的通用方法。此外,它还允许将控制器保存在单独的文件夹中。
  • 你真的应该在这篇文章中添加一些解释,而不仅仅是纯代码。
  • 你真的不应该在这篇文章中添加一些解释,很明显! ◕‿◕
【解决方案3】:

我来自 .Net 世界,通常已完成路由:

/{Controller}/{action}/{id}

看起来像:

/Products/Show/1 OR /Products/Show/Beverages

在 Laravel 中,我完成了这样的路由:

Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='index', $id = null) {
    $controller = ucfirst($controller);
    return APP::make("{$controller}Controller")->$action($id);
});

控制器大致如下所示:

class ProductsController extends BaseController {
    public function Show($id) {
        $products = array( 1 => array("Price" => "$600","Item" => "iPhone 6"),
                           2 => array("Price" => "$700", "Item" => "iPhone 6 Plus") );

        if ($id == null) {
            echo $products[1]["Item"];
        } else {
            echo $products[$id]["Item"];
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多