Php 不推荐使用:在Symfony路由器组件中不应静态调用call_user_func_array()
我使用的symfony路由器组件如下: index.phpPhp 不推荐使用:在Symfony路由器组件中不应静态调用call_user_func_array(),php,symfony,symfony-components,Php,Symfony,Symfony Components,我使用的symfony路由器组件如下: index.php require '../application/core/Core.php'; $request = Request::createFromGlobals(); // Our framework is now handling itself the request $app = new Framework\Core(); $response = $app->handle($request); $response->s
require '../application/core/Core.php';
$request = Request::createFromGlobals();
// Our framework is now handling itself the request
$app = new Framework\Core();
$response = $app->handle($request);
$response->send();
Core.php
namespace Framework;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class Core implements HttpKernelInterface
{
/** @var RouteCollection */
protected $routes;
public function __construct()
{
$this->routes = new RouteCollection();
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
// Load routes from the yaml file
$fileLocator = new FileLocator(array(__DIR__));
$loader = new YamlFileLoader($fileLocator);
$routes = $loader->load('routes.yaml');
// create a context using the current request
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
try {
$attributes = $matcher->match($request->getPathInfo());
print_r($attributes);
$controller = $attributes['_controller'];
$response = call_user_func_array($controller, $attributes);
} catch (ResourceNotFoundException $e) {
$response = new Response('Not found!', Response::HTTP_NOT_FOUND);
}
return $response;
}
public function map($path, $controller) {
$this->routes->add($path, new Route(
$path,
array('controller' => $controller)
));
}
}
在routes.yaml
I中添加:
index:
path: /test/foo
methods: GET
controller: 'App\Catalog\Controller\Home\IndexController::index'
我有一个索引控制器:
namespace App\Catalog\Controller\Home;
class IndexController extends \App\Core\Controller
{
public function index()
{
$this->Language->load('common/home');
$data['url'] = Config::get('URL');
$data['title'] = $this->Language->get('text_title');
//$data['Csrf_Token'] = Csrf::generate('csrf_token');
$this->templates->addData($data, 'common/header');
$headerData['scripts'] = $this->Document->getScripts('header');
$headerData['data'] = $this->loadController('Common\HeaderController')->index();
$this->templates->addData($headerData, 'common/header');
echo $this->templates->render('index/index', $data);
}
}
现在,当我在浏览器中加载此uri:test/foo
时,我看到以下错误:
>
不推荐:call\u user\u func\u array()希望参数1是有效的回调,不应在第42行的/Applications/MAMP/htdocs/test/application/core/core.php中静态调用非静态方法App\Catalog\Controller\Home\IndexController::index()
致命错误:未捕获错误:在/Applications/MAMP/htdocs/test/application/Catalog/Controller/Home/IndexController.php中不在对象上下文中使用$this:16堆栈跟踪:#0[内部函数]:App\Catalog\Controller\Home\IndexController::index('App\Catalog\Con…,'index')#1/Applications/MAMP/htdocs/test/application/core/core.php(42):在第16行的/Applications/MAMP/htdocs/test/application/Controller/Home/IndexController.php中,调用用户函数数组('App\Catalog\Con…',数组)#2/Applications/MAMP/htdocs/test/public/index.php(98):Framework\Core->handle(Object(Symfony\Component\HttpFoundation\Request))#3{main}
Core.php
文件中的第42行是:
$response = call_user_func_array($controller, $attributes);
我怎样才能修正这个错误 修复:
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
$argumentResolver = new HttpKernel\Controller\ArgumentResolver();
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$controller = $controllerResolver->getController($request);
$arguments = $argumentResolver->getArguments($request, $controller);
$response = call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
$response = new Response('Not found!', Response::HTTP_NOT_FOUND);
}
我想你仍然需要
在这里调用用户函数数组