3

Using symfony / routing, need to implement routing for the MVC application. Using the whole Symfony is prohibited, only the library. Controller class:

namespace App\Controllers;
use App\Core\Controller;
class IndexController extends Controller {

    public function IndexAction(){
        $this->View->render('index');
    }

}

view class:

namespace App\Core;

namespace App\Core;

class View{

    public function render($viewName) {
        $viewAry = explode('/', $viewName);
        $viewString = implode(DS, $viewAry);
        if(file_exists('View/site' . $viewString . '.php')) {
            require 'View/site' . $viewString . '.php';
        } else {
            die('The view \"' . $viewName . '\" does not exist.');
        }
    }
}

and Index.php itself from which it all starts:

use App\Controllers\IndexController;

use App\Core\Routing;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

use App\Core\Application;
use Symfony\Component\Routing\Router;


require __DIR__ . '/vendor/autoload.php';

$collection = new RouteCollection();
$collection->add('index', new Route('/', array(
    '_controller' => [IndexController::class, 'IndexAction']
)));

return $collection;

As a result of a request to the application through the postman, I get nothing, what's the problem?

3
  • Hii, one question please what is DS in this line $viewString = implode(DS, $viewAry); ? Commented Oct 6, 2020 at 10:34
  • define('DS', DIRECTORY_SEPARATOR); Commented Oct 6, 2020 at 15:56
  • and how can i fix this? Commented Oct 6, 2020 at 16:53

1 Answer 1

3

In your front controller you are just defining the routes, but not actually processing the request, matching it to a controller or invoking it.

There is a section on this topic in the manual, it uses more symfony components, but can be of help.

You'd have to determine the requested route directly from PATH_INFO instead of using the HttpFoundation component, and then try to match the request to a route.

Here is a very crude implementation:

$collection = new RouteCollection();
$collection->add('index', new Route('/', array(
    '_controller' => [IndexController::class, 'IndexAction']
)));

$matcher = new UrlMatcher($collection, new RequestContext());

// Matcher will throw an exception if no route found
$match = $matcher->match($_SERVER['PATH_INFO']);

// If the code reaches this point, a route was found, extract the corresponding controller
$controllerClass = $match['_controller'][0];
$controllerAction = $match['_controller'][1];

// Instance the controller
$controller = new $controllerClass();
// Execute it
call_user_func([$controller, $controllerAction]);
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.