Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Route::controller('home');
- Route::get('/', function()
- {
- return Redirect::to('home');
- });
- Event::listen('404', function()
- {
- return Response::error('404');
- });
- Event::listen('500', function()
- {
- return Response::error('500');
- });
- 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');
- });
- Route::get('login', array('uses' => 'login@index'));
- Route::post('login', function() {
- //return "login form sent";
- //Get the person data from login
- $credentials = array(
- 'username' => Input::get('username'),
- 'password' => Input::get('password')
- );
- if(Auth::attempt($credentials)):
- //We are logged in and sending the user to home
- return Redirect::to('admin');
- else:
- //Auth fail and we are sending user back so he can log in
- return Redirect::to('login')
- ->with('login_errors', true);
- endif;
- });
- use Admin\Libraries\ModelHelper;
- use Admin\Libraries\Fields\Field;
- use Admin\Libraries\Column;
- use Admin\Libraries\Sort;
- //admin index view
- View::composer('admin.index', function($view)
- {
- //get a model instance that we'll use for constructing stuff
- $modelInstance = ModelHelper::getModel($view->modelName);
- $columns = Column::getColumns($modelInstance);
- $editFields = Field::getEditFields($modelInstance);
- //add the view fields
- $view->modelTitle = Config::get('administrator.models.'.$view->modelName.'.title', $view->modelName);
- $view->modelSingle = Config::get('administrator.models.'.$view->modelName.'.single', $view->modelTitle);
- $view->columns = $columns['columns'];
- $view->includedColumns = $columns['includedColumns'];
- $view->primaryKey = $modelInstance::$key;
- $view->sort = Sort::get($modelInstance)->toArray();
- $view->rows = ModelHelper::getRows($modelInstance, $view->sort);
- $view->editFields = $editFields['arrayFields'];
- $view->dataModel = $editFields['dataModel'];
- $view->filters = ModelHelper::getFilters($modelInstance);
- $view->baseUrl = URL::to_route('admin_index');
- $view->bundleHandles = Config::get('administrator.handle');
- $view->expandWidth = ModelHelper::getExpandWidth($modelInstance);
- $view->modelInstance = $modelInstance;
- $view->model = isset($view->model) ? $view->model : false;
- });
- //validate_admin filter
- Route::filter('validate_admin', function ()
- {
- //get the admin check closure that should be supplied in the config
- $authCheck = Config::get('administrator.auth_check');
- if (!$authCheck())
- {
- $loginUrl = URL::to(Config::get('administrator.login_path', 'user/login'));
- $redirectKey = Config::get('administrator.login_redirect_key', 'redirect');
- $redirectUri = URL::to_route('admin_dashboard');
- return Redirect::to($loginUrl)->with($redirectKey, $redirectUri);
- }
- });
- //validate_model filter
- Route::filter('validate_model', function ()
- {
- $modelName = URI::segment(2);
- $model = ModelHelper::getModelInstance($modelName);
- //if the model doesn't exist at all, redirect to 404
- if (!$model)
- {
- return Response::error('404');
- }
- //if the model does exist, check if this user has permission to access it
- if (!ModelHelper::checkPermission($modelName))
- {
- Redirect::to_route('admin_dashboard');
- }
- });
- /**
- * Admin Routes
- */
- Route::get('admin', array(
- 'as' => 'admin_dashboard',
- 'uses' => 'admin@dashboard',
- 'before' => 'validate_admin', //only needs to validate admin and add assets
- ));
- //The route group for all other requests needs to validate admin, model, and add assets
- Route::group(array('before' => 'validate_admin|validate_model'), function()
- {
- //Model Index
- Route::get('admin/(:any)', array(
- 'as' => 'admin_index',
- 'uses' => 'administrator::admin@index'
- ));
- //Get Item
- Route::get('admin/(:any)/(:num)', array(
- 'as' => 'admin_get_item',
- 'uses' => 'admin@item'
- ));
- //New Item
- Route::get('admin/(:any)/new', array(
- 'as' => 'admin_new_item',
- 'uses' => 'admin@item'
- ));
- //Upload Item
- Route::post('admin/(:any)/upload', array(
- 'as' => 'admin_upload_item',
- 'uses' => 'admin@upload'
- ));
- //Model Images
- Route::get('admin/images', array(
- 'as' => 'admin_images_manager',
- 'uses' => 'admin@images_manager'
- ));
- //Search Relationship Items
- Route::get('admin/(:any)/search_relation/(:any)/(:any)', array(
- 'as' => 'admin_search_relation',
- 'uses' => 'admin@search_relation'
- ));
- //CSRF protection in forms
- Route::group(array('before' => 'csrf'), function()
- {
- //Save Item
- Route::post('admin/(:any)/(:num?)/(:num?)/save', array(
- 'as' => 'admin_save_item',
- 'uses' => 'admin@save'
- ));
- //Delete Item
- Route::post('admin/(:any)/(:num)/delete', array(
- 'as' => 'admin_delete_item',
- 'uses' => 'admin@delete'
- ));
- //Get results
- Route::post('admin/(:any)/results', array(
- 'as' => 'admin_get_results',
- 'uses' => 'admin@results'
- ));
- });
- });
- Route::get('logout', function() {
- Auth::logout();
- return Redirect::to('login');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement