示例#1
0
 /**
  * Get a route path by a given name
  * @param string $routeName
  * @param array $params (optional)
  * @return string
  */
 public final function urlFor($routeName, array $params = array())
 {
     if (null === $this->router) {
         return '';
     }
     return $this->router->urlFor($routeName, $params);
 }
示例#2
0
 public function testUrlFor()
 {
     $this->assertEquals('/somewhere/{name}', $this->router->urlFor('index_name_not_same'));
     $this->assertEquals('/somewhere/foobar', $this->router->urlFor('index_name_not_same', array('name' => 'foobar')));
     // next test covers the 'temp' stored urls.
     $this->assertEquals('/somewhere/foobar', $this->router->urlFor('index_name_not_same', array('name' => 'foobar')));
     $this->assertEquals('missing_path', $this->router->urlFor('missing_path'));
 }
示例#3
0
 /**
  * Run the application.
  */
 public function run()
 {
     if (empty($this->packagesMetadata)) {
         $this->setup();
         if (empty($this->packagesMetadata)) {
             // @codeCoverageIgnoreStart
             $e = new PackagesNotFoundException('Application has no packages.');
             $e->setAdditionalData('Application Path', $this->appDirectory);
             $this->error($e);
             return;
             // @codeCoverageIgnoreEnd
         }
     }
     $this->router = new Router();
     $this->router->setRoutes($this->routes);
     $this->router->setMethod($this->request->getRequestMethod());
     $this->router->setPath($this->request->getRequestUri());
     $this->router->setIp($this->request->getClientIp());
     try {
         $route = $this->router->getRoute();
     } catch (RouteNotFoundException $e) {
         $this->notFound($e);
         return;
     }
     $middleware = false;
     if ($this->hasMiddleware()) {
         $middleware = new MiddlewareProcessor($this->config['Middleware']);
     }
     $package = $this->getPackage($route['package']);
     $runnable = $route['runnable'];
     $runnables = explode('::', $runnable);
     // Check if it's a view script only.
     if ($route['view.directRender'] === true) {
         try {
             if ($middleware) {
                 $middleware->runBefore($this->request, $this->response);
             }
             $view = $this->getViewFor($package['package_name'], $runnables[0], $runnables[1]);
             if ($route['view.layout'] !== false) {
                 $view->setLayout($route['view.layout']);
             }
             $view->setResponse($this->response);
             $view->render();
             if ($middleware) {
                 $middleware->runAfter();
             }
             return;
             // @codeCoverageIgnoreStart
         } catch (\Exception $e) {
             $this->error($e);
             return;
         }
     }
     $classString = $package['namespace'] . '\\src\\' . $runnables[0];
     if (class_exists($classString)) {
         try {
             if ($middleware) {
                 $middleware->runBefore($this->request, $this->response);
             }
             $instance = $this->getRunnable($package['package_name'], $runnables[0], $runnables[1], $classString);
             if (is_callable(array($instance, $runnables[1]))) {
                 if ($route['view.layout'] !== false) {
                     $instance->getView()->setLayout($route['view.layout']);
                 }
                 call_user_func_array(array($instance, $runnables[1]), $route['params']);
                 $instance->finalise();
                 if ($middleware) {
                     $middleware->runAfter();
                 }
                 return;
             }
             // @codeCoverageIgnoreStart
         } catch (\Exception $e) {
             $this->error($e);
             return;
         }
         $this->notFound(new RunnableNotFoundException('No such runnable: ' . $classString . '::' . $runnables[1]));
     }
     $this->notFound(new RunnableNotFoundException('No such runnable: ' . $classString . '::' . $runnables[1]));
 }