How to put controller inside folder in laravel 5.1

2019-07-08 08:05发布

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?

7条回答
来,给爷笑一个
2楼-- · 2019-07-08 08:28

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!

查看更多
太酷不给撩
3楼-- · 2019-07-08 08:32

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楼-- · 2019-07-08 08:37

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!

查看更多
别忘想泡老子
5楼-- · 2019-07-08 08:41

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');
});
查看更多
Fickle 薄情
6楼-- · 2019-07-08 08:45

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.

查看更多
Anthone
7楼-- · 2019-07-08 08:48
php artisan make:controller Admin/ShowDashboardController

File name should be ShowDashboardController.php

查看更多
登录 后发表回答