Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用symfony路由在php应用程序中获得工作的路由_Php_Symfony_Routing - Fatal编程技术网

无法使用symfony路由在php应用程序中获得工作的路由

无法使用symfony路由在php应用程序中获得工作的路由,php,symfony,routing,Php,Symfony,Routing,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\RouteCol

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());
}

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

这里举一个完整的例子:


您的代码所做的只是匹配路由并生成$parameters数组。由您来实例化控制器并使用$parameters中的数据调用action方法。还将由您实际将结果响应对象输出回浏览器。您最好只是按照文档安装。
    <?php

namespace HAPBlog\Controller;

use Symfony\Component\HttpFoundation\Response;

class EntityAddController {

  public function load() {

      return new Response('ENTERS');

  }

}
// 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();