public static function laravel_init() { // Load AutoLoad Aliases \Laravel\Autoloader::alias('\\Cloudmanic\\WarChest\\Libraries\\LaravelAuth', 'LaravelAuth'); \Laravel\Autoloader::alias('\\Cloudmanic\\WarChest\\Libraries\\Me', 'Me'); // Extend the Laravel Auth library to use our own custom driver. \Auth::extend('cloudmanic_auth', function () { return new LaravelAuth(); }); // Set Api auth filter. \Laravel\Routing\Route::filter('api_auth', function () { return CloudAuth::sessioninit(); }); // Build a micro for activating a class or not. We use this in a main navigation // to set the html class to active or not. \Laravel\HTML::macro('is_active', function ($name, $home = false, $class = 'active') { $base = \Laravel\URI::segment(1); // Is the the default route? if ($home && empty($base)) { return $class; } // Compare the segment. if ($base == $name) { return $class; } return ''; }); }
/** * "Forward" the request * * This driver is great for a single server install, * and when debugging your application. * * @param string $method GET, POST, PUT, DELETE, etc. * @param array $segments for example array('account', 'all') * @param array $data the post / put data */ public static function request($method, $segments, $data = array()) { $method = strtoupper($method); if (in_array($method, array('GET', 'POST', 'PUT', 'DELETE'))) { Input::replace($data); } $config = static::config(); $_SERVER['PHP_AUTH_USER'] = $config['username']; $_SERVER['PHP_AUTH_PW'] = $config['password']; list($url, $uri, $query_string) = static::url($segments, $data); $prefix = Config::get('layla.domain.url_prefix'); if (!is_null($prefix)) { $prefix .= '/'; } $response = Route::forward($method, $prefix . $uri); $code = $response->foundation->getStatusCode(); $body = $response->content; return new Response($code, json_decode($body)); }
/** * Test that multiple filters can be assigned to a route. * * @group laravel */ public function testMultipleFiltersCanBeAssignedToARoute() { $_SERVER['test-multi-1'] = false; $_SERVER['test-multi-2'] = false; Filter::register('test-multi-1', function () { $_SERVER['test-multi-1'] = true; }); Filter::register('test-multi-2', function () { $_SERVER['test-multi-2'] = true; }); $route = new Route('GET', '', array('before' => 'test-multi-1|test-multi-2')); $route->call(); $this->assertTrue($_SERVER['test-multi-1']); $this->assertTrue($_SERVER['test-multi-2']); }
| First, define a filter: | | Route::filter('filter', function() | { | return 'Filtered!'; | }); | | Next, attach the filter to a route: | | Router::register('GET /', array('before' => 'filter', function() | { | return 'Hello World!'; | })); | */ Route::filter('before', function () { // Do stuff before every request to your application... }); Route::filter('after', function ($response) { // Do stuff after every request to your application... }); Route::filter('csrf', function () { if (Request::forged()) { return Response::error('500'); } }); Route::filter('auth', function () { if (Auth::guest()) { return Redirect::to('login'); } });