0

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');

  }

}

I am referring to the tutorial given below:

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

But when I try to access the site http://example.com/routes.php/blog/add It gives a blank page. Debugging via PHPStorm shows that it does not enter "EntityAddController" Class What is incorrect in the above code ?

1
  • All your code does is to match a route and generate the $parameters array. It's up to you to instantiate the controller and call the action method using data from $parameters. It will also be up to you to actually output the resulting Response object back to the browser. You might be better off just installing the basic Symfony skeleton per the docs. Commented Oct 10, 2019 at 20:40

1 Answer 1

3

There is no magic behind this process, once you get the route information, you will have to call the configured controller and send the response content.

Take a complete example here:

// 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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.