Advertisement
Guest User

Routes.php

a guest
Jan 8th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. Route::controller('home');
  4. Route::get('/', function()
  5. {
  6.     return Redirect::to('home');
  7. });
  8.  
  9. Event::listen('404', function()
  10. {
  11.     return Response::error('404');
  12. });
  13.  
  14. Event::listen('500', function()
  15. {
  16.     return Response::error('500');
  17. });
  18.  
  19. Route::filter('before', function()
  20. {
  21.     // Do stuff before every request to your application...
  22. });
  23.  
  24. Route::filter('after', function($response)
  25. {
  26.     // Do stuff after every request to your application...
  27. });
  28.  
  29. Route::filter('csrf', function()
  30. {
  31.     if (Request::forged()) return Response::error('500');
  32. });
  33.  
  34. Route::filter('auth', function()
  35. {
  36.     if (Auth::guest()) return Redirect::to('login');
  37. });
  38.  
  39. Route::get('login', array('uses' => 'login@index'));
  40.  
  41. Route::post('login', function() {
  42.     //return "login form sent";
  43.  
  44.     //Get the person data from login
  45.     $credentials = array(
  46.         'username' => Input::get('username'),
  47.         'password' => Input::get('password')
  48.     );
  49.     if(Auth::attempt($credentials)):
  50.         //We are logged in and sending the user to home
  51.         return Redirect::to('admin');
  52.     else:
  53.         //Auth fail and we are sending user back so he can log in
  54.         return Redirect::to('login')
  55.             ->with('login_errors', true);
  56.     endif;
  57. });
  58.  
  59. use Admin\Libraries\ModelHelper;
  60.  
  61. //validate_admin filter
  62. Route::filter('validate_admin', function ()
  63. {
  64.     //get the admin check closure that should be supplied in the config
  65.     $authCheck = Config::get('administrator.auth_check');
  66.  
  67.     if (!$authCheck())
  68.     {
  69.         $loginUrl = URL::to(Config::get('administrator.login_path', 'user/login'));
  70.         $redirectKey = Config::get('administrator.login_redirect_key', 'redirect');
  71.         $redirectUri = URL::to_route('admin_dashboard');
  72.  
  73.         return Redirect::to($loginUrl)->with($redirectKey, $redirectUri);
  74.     }
  75. });
  76.  
  77. //validate_model filter
  78. Route::filter('validate_model', function ()
  79. {
  80.     $modelName = URI::segment(2);
  81.     $model = ModelHelper::getModelInstance($modelName);
  82.  
  83.     //if the model doesn't exist at all, redirect to 404
  84.     if (!$model)
  85.     {
  86.         return Response::error('404');
  87.     }
  88.  
  89.     //if the model does exist, check if this user has permission to access it
  90.     if (!ModelHelper::checkPermission($modelName))
  91.     {
  92.         Redirect::to_route('admin_dashboard');
  93.     }
  94. });
  95.  
  96. /**
  97.  * Admin Routes
  98.  */
  99.  
  100. Route::get('admin', array(
  101.     'as' => 'admin_dashboard',
  102.     'uses' => 'admin@dashboard',
  103.     'before' => 'validate_admin', //only needs to validate admin and add assets
  104. ));
  105.  
  106. //The route group for all other requests needs to validate admin, model, and add assets
  107. Route::group(array('before' => 'validate_admin|validate_model'), function()
  108. {
  109.     //Model Index
  110.     Route::get('admin/(:any)', array(
  111.         'as' => 'admin_index',
  112.         'uses' => 'admin@index'
  113.     ));
  114.  
  115.     //Get Item
  116.     Route::get('admin/(:any)/(:num)', array(
  117.         'as' => 'admin_get_item',
  118.         'uses' => 'admin@item'
  119.     ));
  120.  
  121.     //New Item
  122.     Route::get('admin/(:any)/new', array(
  123.         'as' => 'admin_new_item',
  124.         'uses' => 'admin@item'
  125.     ));
  126.  
  127.     //Upload Item
  128.     Route::any('admin/(:any)/upload', array(
  129.         'as' => 'admin_upload_item',
  130.         'uses' => 'admin@upload'
  131.     ));
  132.  
  133.     //Model Images
  134.     Route::get('admin/(:any)', array(
  135.         'as' => 'admin_images_manager',
  136.         'uses' => 'admin@images_manager'
  137.     ));
  138.  
  139.     //Search Relationship Items
  140.     Route::get('admin/(:any)/search_relation/(:any)/(:any)', array(
  141.         'as' => 'admin_search_relation',
  142.         'uses' => 'admin@search_relation'
  143.     ));
  144.  
  145.     //CSRF protection in forms
  146.     Route::group(array('before' => 'csrf'), function()
  147.     {
  148.         //Save Item
  149.         Route::post('admin/(:any)/(:num?)/(:num?)/save', array(
  150.             'as' => 'admin_save_item',
  151.             'uses' => 'admin@save'
  152.         ));
  153.  
  154.         //Delete Item
  155.         Route::post('admin/(:any)/(:num)/delete', array(
  156.             'as' => 'admin_delete_item',
  157.             'uses' => 'admin@delete'
  158.         ));
  159.  
  160.         //Get results
  161.         Route::post('admin/(:any)/results', array(
  162.             'as' => 'admin_get_results',
  163.             'uses' => 'admin@results'
  164.         ));
  165.     });
  166.  
  167. });
  168.  
  169.  Route::get('logout', function() {
  170.     Auth::logout();
  171.     return Redirect::to('login');
  172. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement