dpcj40970 2018-10-25 11:43
浏览 103

PHP - 中间件\ FastRoute包的nikic / FastRoute路由

I have got these route:

['GET', '/article/{id}', ['SuperBlog\Controller\ArticleController', 'show']],

and I am using Middleware\FastRoute, Middle\RequestHandler and Relay packages to make a Request Handler. Also I am using php-di DI container.

My problem is that if I want to use a route like mentioned above, i gaves me this error:

Deprecated: Non-static method SuperBlog\Controller\ArticleController::show() should not be called statically in

It works well when I don't use methods (like ['GET', '/', 'SuperBlog\Controller\HomeController'],).

My question is how can I make it work? Didn't find any solution. I know that if I make the show method static it will work, but I don't think it's a good idea.

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']],
];
  • 写回答

1条回答 默认 最新

  • doudong3570 2018-12-23 09:27
    关注
    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]);
        }
    });
    

    As it is a multidimensional array you are including from 'routes.php'.

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测