Class "SocialWall/Controller/HomeController" 不存在
Class "SocialWall/Controller/HomeController" does not exist
我的网络应用程序出现此错误:
InvalidArgumentException in ControllerResolver.php line 160: Class "SocialWall/Controller/HomeController" does not exist.
in ControllerResolver.php line 160
at ControllerResolver->createController('SocialWall/Controller/HomeController::indexAction') in ControllerResolver.php line 76
at ControllerResolver->getController(object(Request)) in HttpKernel.php line 136
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 496
at Application->handle(object(Request)) in Application.php line 477
at Application->run() in index.php line 16
我使用 Silex ~2.0,我刚刚重构了我的控制器列表:
我的树:
- root/app/app.php
- root/app/routes.php
- root/src/Controller/HomeController.php
- root/web/index.php
index.php :
<?php
/** Front controller */
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
require __DIR__.'/../app/config/dev.php';
require __DIR__.'/../app/app.php';
require __DIR__.'/../app/routes.php';
$app->run();
routes.php
<?php
// Home page
$app->get('/', "SocialWall/Controller/HomeController::indexAction")->bind('home');
// Detailed info about an article
$app->match('/article/{id}', "SocialWall/Controller/HomeController::articleAction")->bind('article');
// Login form
$app->get('/login', "SocialWall/Controller/HomeController::loginAction")->bind('login');
//...
HomeController.php
<?php
namespace SocialWall\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use SocialWall\Domain\Comment;
use SocialWall\Form\Type\CommentType;
class HomeController {
/**
* Home page controller.
*
* @param Application $app Silex application
*/
public function indexAction(Application $app) {
$articles = $app['dao.article']->findAll();
return $app['twig']->render('index.html.twig', array('articles' => $articles));
}
//...
我的 composer.json :
{
"require": {
"silex/silex": "2.0.*",
"doctrine/dbal": "2.5.*",
"twig/twig": "1.24.*",
"symfony/twig-bridge": "^3.1",
"symfony/security": "^3.1",
"symfony/form": "^3.1",
"symfony/config": "^3.1",
"symfony/translation": "^3.1",
"twig/extensions": "^1.3",
"symfony/validator": "^3.1",
"sorien/silex-pimple-dumper": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"symfony/css-selector": "^3.1",
"symfony/browser-kit": "^3.1",
"monolog/monolog": "^1.21"
},
"autoload": {
"psr-4": {"SocialWall\": "src"}
}
}
怎么了?
[编辑]
好的,我修复它.. 在 route.php 函数的第二个参数中,我已将所有“/”替换为“\”。
最初 phpStorm 在所有红色的 '\' 下划线,所以我将 '\' 替换为 '/'.. 谢谢 phpStorm ...
您指定了错误的命名空间:
namespace Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use SocialWall\Domain\Comment;
use SocialWall\Form\Type\CommentType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller {
...
您还需要扩展 Controller。
编辑#2
您是否也更改了路线:
$app->get('/', "Controller/HomeController::indexAction")->bind('home');
// Detailed info about an article
$app->match('/article/{id}', "Controller/HomeController::articleAction")->bind('article');
// Login form
$app->get('/login', "Controller/HomeController::loginAction")->bind('login');
//...
我的日志(如果有帮助的话)
[2016-09-15 10:03:12] SocialWall.INFO: Matched route "{route}". {"route":"home","route_parameters":{"_controller":"SocialWall/Controller/HomeController::indexAction","_route":"home"},"request_uri":"http://socialwall/","method":"GET"} []
[2016-09-15 10:03:12] SocialWall.CRITICAL: InvalidArgumentException: Class "SocialWall/Controller/HomeController" does not exist. (uncaught exception) at /opt/lampp/htdocs/SocialWall/vendor/symfony/http-kernel/Controller/ControllerResolver.php line 160 {"exception":"[object] (InvalidArgumentException(code: 0): Class \"SocialWall/Controller/HomeController\" does not exist. at /opt/lampp/htdocs/SocialWall/vendor/symfony/http-kernel/Controller/ControllerResolver.php:160)"} []
[2016-09-15 10:03:12] SocialWall.INFO: Matched route "{route}". {"route":"_wdt","route_parameters":{"_controller":"web_profiler.controller.profiler:toolbarAction","token":"b65c04","_route":"_wdt"},"request_uri":"http://socialwall/_profiler/wdt/b65c04","method":"GET"} []
[编辑] 好的,我修复了它。在 route.php 函数的第二个参数中,我将所有的 '/' 替换为 '\'
最初 phpStorm 在红色下划线“\”,所以我用“/”替换了“\”。谢谢 phpStorm ...
我的网络应用程序出现此错误:
InvalidArgumentException in ControllerResolver.php line 160: Class "SocialWall/Controller/HomeController" does not exist.
in ControllerResolver.php line 160
at ControllerResolver->createController('SocialWall/Controller/HomeController::indexAction') in ControllerResolver.php line 76
at ControllerResolver->getController(object(Request)) in HttpKernel.php line 136
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 496
at Application->handle(object(Request)) in Application.php line 477
at Application->run() in index.php line 16
我使用 Silex ~2.0,我刚刚重构了我的控制器列表:
我的树:
- root/app/app.php
- root/app/routes.php
- root/src/Controller/HomeController.php
- root/web/index.php
index.php :
<?php
/** Front controller */
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
require __DIR__.'/../app/config/dev.php';
require __DIR__.'/../app/app.php';
require __DIR__.'/../app/routes.php';
$app->run();
routes.php
<?php
// Home page
$app->get('/', "SocialWall/Controller/HomeController::indexAction")->bind('home');
// Detailed info about an article
$app->match('/article/{id}', "SocialWall/Controller/HomeController::articleAction")->bind('article');
// Login form
$app->get('/login', "SocialWall/Controller/HomeController::loginAction")->bind('login');
//...
HomeController.php
<?php
namespace SocialWall\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use SocialWall\Domain\Comment;
use SocialWall\Form\Type\CommentType;
class HomeController {
/**
* Home page controller.
*
* @param Application $app Silex application
*/
public function indexAction(Application $app) {
$articles = $app['dao.article']->findAll();
return $app['twig']->render('index.html.twig', array('articles' => $articles));
}
//...
我的 composer.json :
{
"require": {
"silex/silex": "2.0.*",
"doctrine/dbal": "2.5.*",
"twig/twig": "1.24.*",
"symfony/twig-bridge": "^3.1",
"symfony/security": "^3.1",
"symfony/form": "^3.1",
"symfony/config": "^3.1",
"symfony/translation": "^3.1",
"twig/extensions": "^1.3",
"symfony/validator": "^3.1",
"sorien/silex-pimple-dumper": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"symfony/css-selector": "^3.1",
"symfony/browser-kit": "^3.1",
"monolog/monolog": "^1.21"
},
"autoload": {
"psr-4": {"SocialWall\": "src"}
}
}
怎么了?
[编辑] 好的,我修复它.. 在 route.php 函数的第二个参数中,我已将所有“/”替换为“\”。 最初 phpStorm 在所有红色的 '\' 下划线,所以我将 '\' 替换为 '/'.. 谢谢 phpStorm ...
您指定了错误的命名空间:
namespace Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use SocialWall\Domain\Comment;
use SocialWall\Form\Type\CommentType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller {
...
您还需要扩展 Controller。
编辑#2 您是否也更改了路线:
$app->get('/', "Controller/HomeController::indexAction")->bind('home');
// Detailed info about an article
$app->match('/article/{id}', "Controller/HomeController::articleAction")->bind('article');
// Login form
$app->get('/login', "Controller/HomeController::loginAction")->bind('login');
//...
我的日志(如果有帮助的话)
[2016-09-15 10:03:12] SocialWall.INFO: Matched route "{route}". {"route":"home","route_parameters":{"_controller":"SocialWall/Controller/HomeController::indexAction","_route":"home"},"request_uri":"http://socialwall/","method":"GET"} []
[2016-09-15 10:03:12] SocialWall.CRITICAL: InvalidArgumentException: Class "SocialWall/Controller/HomeController" does not exist. (uncaught exception) at /opt/lampp/htdocs/SocialWall/vendor/symfony/http-kernel/Controller/ControllerResolver.php line 160 {"exception":"[object] (InvalidArgumentException(code: 0): Class \"SocialWall/Controller/HomeController\" does not exist. at /opt/lampp/htdocs/SocialWall/vendor/symfony/http-kernel/Controller/ControllerResolver.php:160)"} []
[2016-09-15 10:03:12] SocialWall.INFO: Matched route "{route}". {"route":"_wdt","route_parameters":{"_controller":"web_profiler.controller.profiler:toolbarAction","token":"b65c04","_route":"_wdt"},"request_uri":"http://socialwall/_profiler/wdt/b65c04","method":"GET"} []
[编辑] 好的,我修复了它。在 route.php 函数的第二个参数中,我将所有的 '/' 替换为 '\' 最初 phpStorm 在红色下划线“\”,所以我用“/”替换了“\”。谢谢 phpStorm ...