A
A
animr2021-01-14 18:48:33
PHP
animr, 2021-01-14 18:48:33

How to check why null is being passed?

Please help me figure out why this error occurs.

( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Symfony\Component\Routing\Matcher\UrlMatcher::__construct() must be an instance of Symfony\Component\Routing\RouteCollection, null given, called in D:\OSPanel\domains\route.loc\src\System\App.php on line 77 and defined in D:\OSPanel\domains\route.loc\vendor\symfony\routing\Matcher\UrlMatcher.php on line 59
( ! ) TypeError: Argument 1 passed to Symfony\Component\Routing\Matcher\UrlMatcher::__construct() must be an instance of Symfony\Component\Routing\RouteCollection, null given, called in D:\OSPanel\domains\route.loc\src\System\App.php on line 77 in D:\OSPanel\domains\route.loc\vendor\symfony\routing\Matcher\UrlMatcher.php on line 59


There is index.php
<?Php 

  require "../vendor/autoload.php";
  $app = require_once __DIR__ .'/../bootstrap/app.php';
  $app->run();
  
?>


There is app.php
<?Php 
  
  define( "BASEPATH",dirname( __DIR__ ));
  $app = \App\System\App::getInstance( BASEPATH );
  return $app;

?>


Have App.php in another space
<?Php 

  namespace App\System;

  use Symfony\Component\Routing;
  use Symfony\Component\HttpKernel;
  use Symfony\Component\Routing\Router;
  use Symfony\Component\Routing\RequestContext;
  use Symfony\Component\Routing\RouteCollection;
  use Symfony\Component\Routing\Loader\YamlFileLoader;
  use Symfony\Component\Routing\Generator\UrlGenerator;
  use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Config\FileLocator;
  use Symfony\Component\HttpKernel\Controller;
  use Symfony\Component\HttpKernel\Controller\ControllerResolver;

  Class App {

    private $router;
    private $routes;
    private $request;
    private $basePath;
    private $arguments;
    private $controller;
    private $requestContext;

    public static $instance = null;

    public static function getInstance( $basePath ) {
      if ( is_null( static::$instance )) {
        static::$instance = new static( $basePath );
      }
      return static::$instance;
    }

    private function __constructor( $basePath ) {
      $this->basePath = $basePath;
      $this->setRequest();
      $this->setRequestContext();
      $this->setRouter();
      $this->routes = $this->router->getRouteCollection();
    }

    private function setRequest() {
      $this->request = Request::createFromGlobals();
    }

    private function setRequestContext() {
      $this->requestContext = new RequestContext();
      $this->requestContext->fromRequest( $this->request );
    }

    private function getRequest() {
      return $this->request;
    }
    
59		private function getRequestContext() {
      return $this->requestContext;
    }

    private function setRouter() {
      $fileLocator = new FileLocator( array( __DIR__ ));
      $this->router = new Router(
        new YamlFileLoader( $fileLocator ),
        $this->basePath .'/config/routes.yaml',
        array( 'cache_dir' => $this->basePath .'/storage/cache' )
      );
    }

    public function getController() {
      return ( new ControllerResolver())->getController( $this->request );
    }

    public function run() {
77			$matcher = new Routing\Matcher\UrlMatcher( $this->routes, $this->requestContext );

      try {
        $this->request->attributes->add( $matcher->match( $this->request->getPathInfo()));
        $controller = $this->getController();
        dd($controller);
        //$arguments = $this->getArguments($controller);
      } catch ( \Exception $e ) {
        exit( 'error' );
      }
    }
  }
?>


And here is the routes.yaml routes
index_route:
  path: /
  defaults: { _controller: 'App\Http\IndexController::indexAction' }
  
page_route:
  path: /page/{alias}
  defaults: { _controller: 'App\Http\PageController::showAction' }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2021-01-14
@animr

\App\System\App::__constructor=> \App\System\App::__construct
It was necessary to look where it is initialized $this->routesand use the debugger or the usual one there var_dump. Since execution didn't get to that line of code, you would have noticed the typo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question