【问题标题】:Unable to get routing to work in php application using symfony routing无法使用 symfony 路由让路由在 php 应用程序中工作
【发布时间】:2020-02-08 01:44:18
【问题描述】:

index.php

require "vendor/autoload.php";
require "routes.php";

routes.php

<?php
require "vendor/autoload.php";

use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;




try {

    $form_add_route = new Route(
        '/blog/add',
        array(
          'controller' => '\HAPBlog\Controller\EntityAddController',
          'method'=>'load'
        )
    );


    $routes = new RouteCollection();
    $routes->add('blog_add', $form_add_route);

    // Init RequestContext object
    $context = new RequestContext();
    $context->fromRequest(Request::createFromGlobals());

    $matcher = new UrlMatcher($routes, $context);
    $parameters = $matcher->match($context->getPathInfo());

    // How to generate a SEO URL
    $generator = new UrlGenerator($routes, $context);
    $url = $generator->generate('blog_add');
    echo $url;
}

catch (Exception $e) {
    echo '<pre>';
    print_r($e->getMessage());
}

src/Controller/EntityAddController.php

    <?php

namespace HAPBlog\Controller;

use Symfony\Component\HttpFoundation\Response;

class EntityAddController {

  public function load() {

      return new Response('ENTERS');

  }

}

我指的是下面给出的教程:

https://code.tutsplus.com/tutorials/set-up-routing-in-php-applications-using-the-symfony-routing-component--cms-31231

但是当我尝试访问该网站时http://example.com/routes.php/blog/add 它给出了一个空白页。 通过 PHPStorm 调试显示没有进入“EntityAddController”类 上面的代码哪里不对?

【问题讨论】:

  • 您的代码所做的只是匹配一个路由并生成 $parameters 数组。由您来实例化控制器并使用 $parameters 中的数据调用操作方法。您还可以将生成的 Response 对象实际输出回浏览器。根据文档安装 basic Symfony skeleton 可能会更好。

标签: php symfony routing


【解决方案1】:

这个过程没有什么神奇的,一旦你得到路由信息,你就必须调用配置的控制器并发送响应内容。

在这里举一个完整的例子:

// controllers.php

class BlogController
{
    public static function add(Request $request)
    {
        return new Response('Add page!');
    }
}

// routes.php

$routes = new RouteCollection();
$routes->add('blog_add', new Route('/blog/add', [
    'controller' => 'BlogController::add',
]));

// index.php

$request = Request::createFromGlobals();
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);

try {
    $attributes = $matcher->match($request->getPathInfo());

    $response = $attributes['controller']($request);
} catch (ResourceNotFoundException $exception) {
    $response = new Response('Not Found', 404);
} catch (Exception $exception) {
    $response = new Response('An error occurred', 500);
}

$response->send();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多