Pop\Mvc\Router::route PHP Méthode

route() public méthode

Route to the correct controller
public route ( Project $project = null ) : void
$project Pop\Project\Project
Résultat void
    public function route(\Pop\Project\Project $project = null)
    {
        if (null !== $project) {
            $this->project = $project;
        }
        // If the request isn't root '/', traverse the URI path
        if ($this->request->getPath(0) != '') {
            $this->controllerClass = $this->traverseControllers($this->controllers);
            // Else, use root '/'
        } else {
            $this->controllerClass = isset($this->controllers['/']) ? $this->controllers['/'] : null;
        }
        // If found, create the controller object
        if (null !== $this->controllerClass && class_exists($this->controllerClass)) {
            // Push the real base path and URI into the request object
            $realBasePath = $this->request->getBasePath() . $this->basePath;
            $realUri = substr($this->request->getFullUri(), strlen($this->request->getBasePath() . $this->basePath));
            // Create the controller object
            $this->controller = new $this->controllerClass($this->request->setRequestUri($realUri, $realBasePath), null, $this->project);
            // Trigger any route events
            if (null !== $this->project) {
                if (null !== $this->project->getEventManager()->get('route')) {
                    $this->project->log('[Event] Route', time(), \Pop\Log\Logger::NOTICE);
                }
                $this->project->getEventManager()->trigger('route', array('router' => $this));
            }
            // Else, trigger any route error events
        } else {
            if (null !== $this->project) {
                if (null !== $this->project->getEventManager()->get('route.error')) {
                    $this->project->log('[Event] Route Error', time(), \Pop\Log\Logger::NOTICE);
                }
                $this->project->getEventManager()->trigger('route.error', array('router' => $this));
            }
        }
    }

Usage Example

Exemple #1
0
 public function testRoute()
 {
     $r = new Router(array('/' => 'Pop\\Mvc\\Controller'));
     $r->route(new Project(new Config(array())));
     $this->assertInstanceOf('Pop\\Mvc\\Controller', $r->controller());
     $this->assertInstanceOf('Pop\\Project\\Project', $r->project());
 }
All Usage Examples Of Pop\Mvc\Router::route