laravel 流明框架路由不起作用

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

lumen framework routing not working

phplaravellumen

提问by refear99

I use the Lumen framework for first time, the route /to my HomeController is not working.

我第一次使用 Lumen 框架,/到我的 HomeController的路由不起作用。

This is my route.php:

这是我的 route.php:

$app->get('/', 'HomeController@index');

But I get the following error:

但我收到以下错误:

[2015-04-17 07:03:41] lumen.ERROR: exception 'ReflectionException' with message 'Class HomeController does not exist' in /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php:776

Stack trace:
#0 /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php(776): ReflectionClass->__construct('HomeController')
#1 /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php(656): Illuminate\Container\Container->build('HomeController', Array)
#2 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(358): Illuminate\Container\Container->make('HomeController', Array)
#3 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1184): Laravel\Lumen\Application->make('HomeController')
#4 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1157): Laravel\Lumen\Application->callControllerAction(Array)
#5 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1142): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#6 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1120): Laravel\Lumen\Application->handleArrayBasedFoundRoute(Array)
#7 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1058): Laravel\Lumen\Application->handleFoundRoute(Array)
#8 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1006): Laravel\Lumen\Application->dispatch(NULL)
#9 /Users/refear99/Web/qingsongchou_api/public/index.php(28): Laravel\Lumen\Application->run()
#10 {main}  

This is my HomeController.php in /app/Http/Controllers/

这是我在 /app/Http/Controllers/ 中的 HomeController.php

<?php namespace App\Http\Controllers;

class HomeController extends Controller {

public function index()
{
    echo 123;
}

}

What could the problem be?

可能是什么问题?

回答by lukasgeiter

You have to use the fully qualified classname:

您必须使用完全限定的类名:

$app->get('/', 'App\Http\Controllers\HomeController@index');

ORwrap all routes in a group (which is actually how it's done under the hood in Laravel 5)

将所有路线包装在一个组中(这实际上是 Laravel 5 中的幕后完成方式)

$app->group(['namespace' => 'App\Http\Controllers'], function($group){

    $group->get('/', 'HomeController@index');
    $group->get('foo', 'FooController@index');

});

回答by Wayne Ashley Berry

It appears to be undocumented right now, but you need to use the full namespace path to the controller.

它现在似乎没有记录,但您需要使用控制器的完整命名空间路径。

So your route would look like this:

所以你的路线看起来像这样:

$app->get('/', 'App\Http\Controllers\HomeController@index');

The difference lies in the RouteServiceProvider that ships with Laravel, which can be found in app/Providers/RouteServiceProvider.php, check out the map method, it looks as follows

不同之处在于Laravel自带的RouteServiceProvider,可以在app/Providers/RouteServiceProvider.php中找到,查看map方法,如下所示

$router->group(['namespace' => $this->namespace], function($router)
{
    require app_path('Http/routes.php');
});

So all of your application routes are actually grouped under a default namespace, which is usually App\Http\Controllers.

所以你所有的应用程序路由实际上都分组在一个默认的命名空间下,通常是 App\Http\Controllers。

Hope that helps!

希望有帮助!

回答by Thomas Venturini

Take a look at the file /bootstrap/app.phpThere you can make some settings. Also there, at the bottom of the file, you will find the following lines.

看看文件/bootstrap/app.php那里你可以做一些设置。此外,在文件底部,您会找到以下几行。

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

return $app;

Which should serve your calls with the right namespace.

哪个应该为您的呼叫提供正确的命名空间。

Also you can activate the .env settings right there :)

你也可以在那里激活 .env 设置:)

Take a look at this Post https://mattstauffer.co/blog/introducing-lumen-from-laravel

看看这篇文章https://mattstauffer.co/blog/introducing-lumen-from-laravel

Hope this helps someone! :)

希望这对某人有帮助!:)