How to put controller inside folder in laravel 5.1

2019-07-08 07:56发布

问题:

I am new to laravel. I am trying to organise my controller by putting it inside a folder, but it doesn't seem to work.

My folder structure is like this:

/app
    /Http
        /Controllers
            /Admin
                ShowDashboard.php

My ShowDashboard.php file is like this:

<?php namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class ShowDashboard extends Controller {

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function init()
    {
        return 'Hi there!';
    }

}

My route is like this

Route::get('/admin', 'Admin\ShowDashboard@init');

When I tred to access http://localhost:8000/admin I get the following error:

Class App\Http\Controllers\Admin\ShowDashboard does not exist

My autolaoder section:

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    }

Am I missing something?

回答1:

The best way to create a controller is to use the built in Laravel utility, Artisan. From a command prompt, browse to the directory your laravel project is located. For example: c:\development\htdocs\www.example.dev

At the prompt, type: php artisan make:controller admin/showDashboard --plain

This will generate a file named showDashboard.php within an admin directory under your controllers. The file will have the following code by default:

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class showDashboard extends Controller
{
    //
}

Now that you have created your controller, add a method for init:

public function init()
{
    return 'Hi there!';
}

Your controller will now look like this:

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class showDashboard extends Controller
{
    public function init()
    {
        return 'Hi there!';
    }
}

Now, setup your route in your routes.php as follows:

route::get('admin', 'admin\showDashboard@init');

Save your work, and launch your page. When browsing to www.example.dev/admin you should see the message: Hi there!

I hope this helps!



回答2:

I don't know why this was happening, but adding this in my route fixed it.

Route::group(['namespace' => 'Admin'], function()
{
    // Controllers Within The "App\Http\Controllers\Admin" Namespace

   Route::get('/admin','ShowAdminDashboard@index');
});


回答3:

Everything is already explained but one more try can be done by adding controller suffix to showDashboard and run composer dump-autoload.

I think then your controller will run.

Rename your controller ShowDashboardController



回答4:

php artisan make:controller Admin/ShowDashboardController

File name should be ShowDashboardController.php



回答5:

I don't see anything wrong with what you posted. If you changed the namespace-to-folder mappings in composer.json, make sure you ran the 'composer dump-autoload' command.



回答6:

The following code is working.. Try once

created a file ShowDashboard.php in folder admin like app/http/controller

now , ShowDashboard.php

<?php 
namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;

class ShowDashboard extends Controller {
   public function init()
    {
        return 'Hi there!';
    }

}

Added Route::get('admin', 'admin\ShowDashboard@init'); in routes.php

and then run composer update on cmd.. Then run http://localhost:8000/admin . it says.. Hi there!



回答7:

Create a new controller in subfolder, for example: app/Http/Controllers/User/UserController.php

In this controller, at the end of namespace must include folder name

Like this: namespace App\Http\Controllers\User;

The important thing is under namespace must write use App\Http\Controllers\Controller;

finally in routes.php Route::get ( '/user', 'User\UserController@login' );

UserController.php contents:

<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
class UserController extends Controller {
    public function login() {
        return 'this login';
    }
}

routes.php contents:

Route::get ( '/user/login', 'User\UserController@login' );

// or use this
Route::group ( [
        'namespace' => 'User'
], function () {
    Route::get ( '/user/login', 'UserController@login' );
} );