Example #1
1
 public function testGenerateWithNonexistingRoute()
 {
     try {
         $this->router->generate('nonexisting_route');
         $this->fail('Should trigger an exception on nonexisting named route');
     } catch (Exception $e) {
         $this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage());
     }
 }
Example #2
1
 /**
  * Generates url for a given route.
  *
  * @param string $route
  * @param array $params
  * @return string
  * @throws \Exception
  */
 public function generate($route, array $params = [])
 {
     return $this->altoRouter->generate($route, $params);
 }
Example #3
1
<h3>Current request: </h3>
<pre>
	Target: <?php 
var_dump($match['target']);
?>
	Params: <?php 
var_dump($match['params']);
?>
	Name: 	<?php 
var_dump($match['name']);
?>
</pre>

<h3>Try these requests: </h3>
<p><a href="<?php 
echo $router->generate('home');
?>
">GET <?php 
echo $router->generate('home');
?>
</a></p>
<p><a href="<?php 
echo $router->generate('users_show', array('id' => 5));
?>
">GET <?php 
echo $router->generate('users_show', array('id' => 5));
?>
</a></p>
<p><form action="<?php 
echo $router->generate('users_do', array('id' => 10, 'action' => 'update'));
?>
Example #4
0
 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
 {
     $match = $this->router->match($request->getPathInfo());
     $route = substr($request->getPathInfo(), strlen(rtrim($this->config['baseDir'], '/')));
     if ($match) {
         $tokenValid = false;
         $jwtCookie = $this->config['jwt']['cookieName'];
         $jwtKey = $this->config['jwt']['key'];
         // check token from cookie
         if ($request->cookies->has($jwtCookie)) {
             $jwt = $request->cookies->get($jwtCookie);
             try {
                 $decoded = JWT::decode($jwt, $jwtKey, ['HS256']);
                 if ($decoded->e > time()) {
                     $tokenValid = true;
                     $this->auth->init($decoded->uid);
                 }
             } catch (\Exception $e) {
                 $tokenValid = false;
                 if (!$catch) {
                     throw $e;
                 }
                 $response = $this->dispatcher->dispatch('Home#error', ['message' => '[' . $e->getCode() . '] ' . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>']);
                 $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
                 return $response;
             }
         }
         $allowed = false;
         $isPublic = false;
         foreach ($this->config['publicArea'] as $publicRoute) {
             if (preg_match('/^' . addcslashes($publicRoute, '/') . '/', $route)) {
                 $isPublic = true;
                 break;
             }
         }
         if ($match['name'] == 'home') {
             $isPublic = true;
         }
         if ($isPublic) {
             if ($route == '/login' && $tokenValid) {
                 return new RedirectResponse($this->router->generate('dashboard'));
             }
             $allowed = true;
         } else {
             $allowed = $tokenValid;
         }
         if ($allowed) {
             $this->app->setRouteMatch($match);
             return $this->app->handle($request, $type, $catch);
         } else {
             $this->flash->warning('Sesi Anda telah habis atau Anda tidak berhak mengakses halaman ini, silakan login terlebih dahulu!');
             $response = $this->dispatcher->dispatch('User#login', []);
             $response->setStatusCode(Response::HTTP_UNAUTHORIZED);
             return $response;
         }
     }
     $response = $this->dispatcher->dispatch('Home#error', ['message' => 'Halaman tidak ditemukan: ' . $route]);
     $response->setStatusCode(Response::HTTP_NOT_FOUND);
     return $response;
 }
Example #5
0
 /**
  *
  * {@inheritdoc}
  *
  * @see AltoRouter::generate()
  */
 public function generate($routeName, array $params = array())
 {
     $url = parent::generate($routeName, $params);
     if (!empty($this->base_url)) {
         $url = $this->base_url . $url;
     }
     return $url;
 }
Example #6
0
 public function generate($routeName, $params = array())
 {
     return $this->altoRouter->generate($routeName, $params);
 }
Example #7
-1
 require SCRIPTROOT . 'dbaccess.php';
 $dbconnection = CreateDBConnection();
 // setup login session
 require SCRIPTROOT . 'loginsession.php';
 $session = new LoginSession();
 // make sure we meet authentication requirements
 $requiresauthentication = isset($target['usersonly']) && $target['usersonly'];
 $requiresadmin = isset($target['adminsonly']) && $target['adminsonly'];
 $requiresguest = isset($target['guestsonly']) && $target['guestsonly'];
 // check if we need to be authenticated
 if ($requiresauthentication || $requiresadmin) {
     if (!$session->IsLoggedIn() || $requiresadmin && ($session->GetStatus() != AccountStatus::Admin && $session->GetStatus() != AccountStatus::Owner)) {
         if ($apirequest) {
             ErrorPage(401);
         } else {
             header('Location: ' . $routes->generate('login'));
         }
         die;
     }
 } else {
     if ($requiresguest) {
         if ($session->IsLoggedIn()) {
             if ($apirequest) {
                 ErrorPage(401);
             } else {
                 header('Location: ' . $routes->generate('account'));
                 die;
             }
         }
     }
 }