Example #1
0
 public function testRequest()
 {
     $r = new Request();
     $r->setRequestUri('/test', '/admin');
     $this->assertEquals('/admin', $r->getBasePath());
     $this->assertEquals('/test', $r->getRequestUri());
     $this->assertEquals('/admin/test', $r->getFullUri());
     $this->assertFalse($r->isFile());
     $this->assertFalse($r->isSecure());
     $this->assertEquals('', $r->getDocRoot());
     $this->assertEquals('/admin', $r->getFullPath());
     $this->assertNull($r->getFilename());
     $this->assertEquals('http', $r->getScheme());
     $this->assertTrue(is_array($r->getQuery()));
     $this->assertNull($r->getQuery('test'));
     $this->assertTrue(is_array($r->getPost()));
     $this->assertNull($r->getPost('test'));
     $this->assertTrue(is_array($r->getPut()));
     $this->assertNull($r->getPut('test'));
     $this->assertTrue(is_array($r->getPatch()));
     $this->assertNull($r->getPatch('test'));
     $this->assertTrue(is_array($r->getDelete()));
     $this->assertNull($r->getDelete('test'));
     $this->assertTrue(is_array($r->getCookie()));
     $this->assertNull($r->getCookie('test'));
     $this->assertTrue(is_array($r->getServer()));
     $this->assertNull($r->getServer('test'));
     $this->assertTrue(is_array($r->getEnv()));
     $this->assertNull($r->getEnv('test'));
 }
 /**
  * Dispatch the controller based on the action
  *
  * @param  string $action
  * @throws \Pop\Mvc\Exception
  * @return \Pop\Mvc\Controller
  */
 public function dispatch($action = 'index')
 {
     if (method_exists($this, $action)) {
         if (null !== $this->project->logger()) {
             $this->project->log("Dispatch ['" . get_class($this) . "']->" . $action . "\t" . $this->request->getRequestUri() . "\t" . $this->request->getFullUri(), time());
         }
         $this->{$action}();
     } else {
         throw new Exception('That action is not defined in the controller.');
     }
 }
Example #3
0
 /**
  * Route to the correct controller
  *
  * @param  \Pop\Project\Project $project
  * @return 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));
         }
     }
 }