Задать вопрос
@animr

Как проверить почему передается значение null?

Помогите разобраться почему появляется ошибка
( ! ) 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


Есть index.php
<?Php 

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


Есть app.php
<?Php 
	
	define( "BASEPATH",dirname( __DIR__ ));
	$app = \App\System\App::getInstance( BASEPATH );
	return $app;

?>


Есть App.php в другом пространстве
<?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' );
			}
		}
	}
?>


А вот маршруты routes.yaml
index_route:
	path: /
	defaults: { _controller: 'App\Http\IndexController::indexAction' }
	
page_route:
	path: /page/{alias}
	defaults: { _controller: 'App\Http\PageController::showAction' }
  • Вопрос задан
  • 104 просмотра
Подписаться 1 Простой Комментировать
Решения вопроса 1
BoShurik
@BoShurik Куратор тега Symfony
Symfony developer
\App\System\App::__constructor => \App\System\App::__construct

Надо было посмотреть где инициализируется $this->routes и там воспользоваться дебагером или обычным var_dump. Так как выполнение до этой строчки кода не дошло, вы бы заметили опечатку.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы