路由到子文件夹中的控制器在 Laravel 4 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14504082/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:31:19  来源:igfitidea点击:

Route to controller in subfolder not working in Laravel 4

phpcontrollerrouteslaravellaravel-4

提问by Clifford James

I was updating my Laravel 3 app to Laravel 4 when I hit this problem...

当我遇到这个问题时,我正在将我的 Laravel 3 应用程序更新到 Laravel 4 ......

Routes I have tried:

我尝试过的路线:

Route::get('backend/login', 'backend/UserController@login');
Route::get('backend/login', 'backend.UserController@login');

回答by Federico Stango

I had a similar issue just a few hours ago and had to play a little bit with it to have it working.

就在几个小时前,我遇到了类似的问题,不得不稍微玩一下才能让它工作。

Routes:

路线:

Route::group(array('prefix' => 'admin'), function() {
    Route::resource('/', 'admin\DashboardController');
});

In "controllers/admin" i put the DashboardController:

在“控制器/管理员”中,我放置了 DashboardController:

namespace admin;

use Illuminate\Support\Facades\View;

class DashboardController extends \BaseController {

    public function index()
    {
        return View::make('admin/dashboard');
    }

}

That did the trick on Laravel 4. Hope you find it useful enough. :)

这在 Laravel 4 上起到了作用。希望你觉得它足够有用。:)

回答by Pierre Broucz

At the moment, in Laravel 4 Beta 1, you can "only ?" use namespace.

目前,在 Laravel 4 Beta 1 中,您可以“仅 ?” 使用命名空间。

For exemple here in your controller file: app/controllers/backend/UserController.php

例如在您的控制器文件中: app/controllers/backend/UserController.php

<?php namespace Controllers\Backend;

use Illuminate\Routing\Controllers\Controller;

class UserController extends Controller {

    // Note extends Controller and not BaseController

    // Your stuff
}
?>

So after, in file: app/routes.php :

所以之后,在文件:app/routes.php 中:

<?php
Route::get('backend/login', 'Controllers\Backend\UserController@login');

I don't know if is the better way, but working here. Edit & dump-autoload "composer.json" seems not work actualy.

我不知道是否是更好的方法,但在这里工作。编辑和转储自动加载“composer.json”似乎实际上不起作用。

If someone can improve that, he will make my day ! :)

如果有人能改进这一点,他会让我开心!:)

回答by facundofarias

If you are gonna use Laravel 4, perhaps you should take a look of this: You can specify the namespace to be used on a group of routes, as you can see here: http://www.laravel-tricks.com/tricks/route-group-namespacing

如果你打算使用 Laravel 4,也许你应该看看这个:你可以指定在一组路由上使用的命名空间,你可以在这里看到:http: //www.laravel-tricks.com/tricks /路由组命名空间

So in your sample:

所以在你的样本中:

Route::group(array('prefix' => 'backend', 'namespace' => 'backend'), function()
{

    Route::get('login', 'UserController@login');

});

It works like a charm :)

它就像一个魅力:)

I've been using it, and are quite good, it helps you keep your code cleaner and more understandable. Give it a try!

我一直在使用它,并且非常好,它可以帮助您保持代码更清晰,更易于理解。试一试!

回答by Hassan Jamal

My Admin Controller in app/controllers directory

app/controllers 目录中的 My Admin Controller

class AdminController extends BaseController {
/**.
 * @return \AdminController
 */
    public function __construct()
    {

    }
}

Now I have a folder named admin in controllers folder i.e app/controllers/admin and I have another controller there named AdminDashboardController.php

现在我在控制器文件夹中有一个名为 admin 的文件夹,即 app/controllers/admin 并且我有另一个名为 AdminDashboardController.php 的控制器

class AdminDashboardController extends AdminController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
    public function getIndex()
    {
    return View::make('admin/dashboard');
    }
    }

And Lastly My Route.php file

最后是我的 Route.php 文件

Route::group(array('prefix' => 'admin'), function()
{
    # Admin Dashboard
    Route::controller('/', 'AdminDashboardController');
 });

Hope this helps ..:-)

希望这可以帮助 ..:-)

回答by facundofarias

As explained here, with Laravel 4.1 you can specify the namespace to be used on a group of routes, as you can see here: http://www.laravel-tricks.com/tricks/route-group-namespacing

如此处所述,使用 Laravel 4.1,您可以指定要在一组路由上使用的命名空间,如您在此处所见:http: //www.laravel-tricks.com/tricks/route-group-namespacing

I've been using it, and are quite good, it helps you keep your code cleaner and more understandable. Give it a try!

我一直在使用它,并且非常好,它可以帮助您保持代码更清晰,更易于理解。试一试!

回答by Arni Gudjonsson

I recommend doing

我建议做

Route::group(array('prefix' => 'backend'), function() {
    // Responds to Request::root() . '/backend/user'
    Route::resource('login', 'UserController');
});

see more info here

在这里查看更多信息

Laravel 4 nested resource controllers Route::resource('admin/photo', 'PhotoController'); not working

Laravel 4 嵌套资源控制器 Route::resource('admin/photo', 'PhotoController'); 不工作

回答by Kevin Op den Kamp

You could also put your backend/admin panel in a package..fruit for thought :)

你也可以把你的后端/管理面板放在一个包中......水果思考:)