Route to controller in subfolder not working in Laravel 4

我是研究僧i 提交于 2020-01-13 08:49:26

问题


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

Routes I have tried:

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

回答1:


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:

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. :)




回答2:


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

For exemple here in your controller file: 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 :

<?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.

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




回答3:


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

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!




回答4:


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




回答5:


My Admin Controller in app/controllers directory

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

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::group(array('prefix' => 'admin'), function()
{
    # Admin Dashboard
    Route::controller('/', 'AdminDashboardController');
 });

Hope this helps ..:-)




回答6:


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

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




回答7:


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



来源:https://stackoverflow.com/questions/14504082/route-to-controller-in-subfolder-not-working-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!