4

I have mvc php cms like this folder structure:

application
---admin
--------controller
--------model
--------view
--------language
---catalog
--------controller
------------------IndexController.php
--------model
--------view
--------language
core
--------controller.php
//...more
public
--------index.php
vendor

I install symfony/router component for help my route url using composer json:

{
  "autoload": {
    "psr-4": {"App\\": "application/"}
  },
  "require-dev":{
    "symfony/routing" : "*"
  }
}

Now with route documents I add this code for routing in index.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;

$route = new Route('/index', array('_controller' => 'App\Catalog\Controller\IndexController\index'));
$routes = new RouteCollection();
$routes->add('route_name', $route);

$context = new RequestContext('/');

$matcher = new UrlMatcher($routes, $context);

$parameters = $matcher->match('/index');

In My IndexController I have :

namespace App\Catalog\Controller;

class IndexController {

    public function __construct()
    {
        echo 'Construct';
    }


    public function index(){
        echo'Im here';
    }
}

Now in Action I work in this url: localhost:8888/mvc/index and can't see result : Im here IndexController.

How do symfony routing url work and find controller in my mvc structure? thank for any practice And Help.

1 Answer 1

1

The request context should be populated with the actual URI that's hitting the application. Instead of trying to do this yourself you can use the HTTP Foundation package from symfony to populate this:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;

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

It's also documented here: https://symfony.com/doc/current/components/routing.html#components-routing-http-foundation

After the matching ($parameters = $matcher->match('/index');) you can use the _controller key of the parameters to instantiate the controller and dispatch the action. My suggestion would be to replace the last \ with a different symbol for easy splitting, like App\Controller\Something::index.

You can then do the following:

list($controllerClassName, $action) = explode($parameters['_controller']);
$controller = new $controllerClassName();
$controller->{$action}();

Which should echo the response you have in your controller class.

Sign up to request clarification or add additional context in comments.

4 Comments

I add "symfony/http-foundation": "*" using composer json and add your code But I cant see reaction!
You should add it using composer require symfony/http-foundation. Did you check if the package was actually installed? And maybe paste the full example.
Ok so, I think I know what else is going on. You're not actually dispatching the action. I'll update my answer.
@NewCod3r I've updated the instructions which should fix your issue. If I'm honest I would suggest you to just use Symfony as a whole or something like Laravel which provides more convenience in terms of simple dispatching of http-related actions.

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.