如何将控制器放入 Laravel 5.1.3 的文件夹中?

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

How to put controller inside folder in laravel 5.1.3?

phplaravellaravel-5laravel-routing

提问by Gautam Kumar

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.

我是 Laravel 的新手。我试图通过将它放在一个文件夹中来组织我的控制器,但它似乎不起作用。

My folder structure is like this:

我的文件夹结构是这样的:

/app
    /Http
        /Controllers
            /Admin
                ShowDashboard.php

My ShowDashboard.phpfile is like this:

我的ShowDashboard.php文件是这样的:

<?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/adminI get the following error:

当我尝试访问http://localhost:8000/admin 时,出现以下错误:

Class App\Http\Controllers\Admin\ShowDashboarddoes not exist

App\Http\Controllers\Admin\ShowDashboard不存在

My autolaoder section:

我的自动装载机部分:

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

Am I missing something?

我错过了什么吗?

回答by Adrian Hunter

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

创建控制器的最佳方法是使用内置的 Laravel 实用程序 Artisan。在命令提示符下,浏览到 Laravel 项目所在的目录。例如:c:\development\htdocs\www.example.dev

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

在提示符下,键入: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:

这将在您的控制器下的 admin 目录中生成一个名为 showDashboard.php 的文件。默认情况下,该文件将具有以下代码:

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

现在您已经创建了控制器,为 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:

现在,在你的 routes.php 中设置你的路由,如下所示:

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!

保存您的工作,并启动您的页面。浏览 www.example.dev/admin 时,您应该会看到以下消息:您好!

I hope this helps!

我希望这有帮助!

回答by Gautam Kumar

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');
});

回答by Anshul

Everything is already explained but one more try can be done by adding controllersuffix to showDashboardand run composer dump-autoload.

一切都已经解释过了,但可以通过添加controller后缀showDashboard和 run来再试一次composer dump-autoload

I think then your controller will run.

我想你的控制器会运行。

Rename your controller ShowDashboardController

重命名您的控制器ShowDashboardController

回答by Ramesh

php artisan make:controller Admin/ShowDashboardController

File name should be ShowDashboardController.php

文件名应该是 ShowDashboardController.php

回答by Max Sky

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

在子文件夹中创建一个新的控制器,例如: app/Http/Controllers/User/UserController.php

In this controller, at the end of namespacemust include folder name

在此控制器中,末尾namespace必须包含文件夹名称

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

像这样: namespace App\Http\Controllers\User;

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

重要的是namespace下一定要写 use App\Http\Controllers\Controller;

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

最后在routes.php Route::get ( '/user', 'User\UserController@login' );

UserController.phpcontents:

UserController.php内容:

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

routes.phpcontents:

route.php内容:

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

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

回答by Itz Raghu

The following code is working.. Try once

以下代码正在运行..尝试一次

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

在文件夹 admin 中创建一个文件ShowDashboard.php,如 app/http/controller

now , ShowDashboard.php

现在, 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

Route::get('admin', 'admin\ShowDashboard@init');routes.php中添加

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

然后composer update在 cmd 上运行 .. 然后运行http://localhost:8000/admin. 它说..你好!

回答by GPicazo

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.

我没有看到你发布的内容有什么问题。如果您在 composer.json 中更改了命名空间到文件夹的映射,请确保您运行了“composer dump-autoload”命令。