[英]PHP - nikic/FastRoute routes for Middlewares\FastRoute package
我有這些路線:
['GET', '/article/{id}', ['SuperBlog\Controller\ArticleController', 'show']],
我正在使用Middleware\\FastRoute
, Middle\\RequestHandler
和Relay
包來制作請求處理程序。 我也在用php-di
DI容器。
我的問題是,如果我想使用上述路由,則會給我這個錯誤:
Deprecated: Non-static method SuperBlog\\Controller\\ArticleController::show() should not be called statically in
當我不使用方法(例如['GET', '/', 'SuperBlog\\Controller\\HomeController'],
)時,它會很好地工作。
我的問題是如何使它起作用? 找不到任何解決方案。 我知道,如果將show
方法設為靜態,它會起作用,但是我認為這不是一個好主意。
bootstrap.php中
/**
* Routing
*/
$routes = simpleDispatcher(function (RouteCollector $r){
$routes = include('routes.php');
foreach ($routes as $route) {
$r->addRoute($route[0], $route[1], $route[2]);
}
});
$middlewareQueue[] = new FastRoute($routes);
$middlewareQueue[] = new RequestHandler($container);
$requestHandler = new Relay($middlewareQueue);
$response = $requestHandler->handle(ServerRequestFactory::fromGlobals());
$emitter = new SapiEmitter();
return $emitter->emit($response);
ArticleController.php
class ArticleController
{
/**
* @var ArticleRepository
*/
private $articleRepository;
/**
* @var Twig_Environment
*/
private $twig;
/**
* @var ResponseInterface
*/
private $response;
public function __construct(ArticleRepository $articleRepository, Twig_Environment $twig, ResponseInterface $response) {
$this->articleRepository = $articleRepository;
$this->twig = $twig;
$this->response = $response;
}
public function show($request) {
$article = $this->articleRepository->get($request->getAttribute('id'));
$this->response->getBody()->write($this->twig->render('article.twig',[
'article' => $article,
]));
return $this->response;
}
}
routes.php文件
return [
['GET', '/', 'SuperBlog\Controller\HomeController'],
['GET', '/article/{id}', ['SuperBlog\Controller\ArticleController', 'show']],
];
Try this:
/**
* Routing
*/
$routes = simpleDispatcher(function (RouteCollector $r){
$routes = include('routes.php');
foreach ($routes as $key => $route) {
$r->addRoute($route[$key][0], $route[$key][1], $route[$key][2]);
}
});
由於它是一個多維數組,因此包含在“ routes.php”中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.