From c8259b0dcfa024630aedd74ade14eb0681a93ef6 Mon Sep 17 00:00:00 2001 From: Theo Weijmer Date: Fri, 22 Dec 2017 09:08:16 +0100 Subject: [PATCH] divers --- application/config/autoload.php | 2 +- application/config/config.php | 2 +- application/config/hooks.php | 10 ++++ application/controllers/Login.php | 2 +- application/controllers/Main.php | 6 +- application/hooks/EloquentHook.php | 60 +++++++++++++++++++ .../beheer/{User.php => User_model.php} | 2 +- application/models/eloquent/Page.php | 31 ++++++++++ application/models/eloquent/User.php | 18 ++++++ composer.json | 8 ++- 10 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 application/hooks/EloquentHook.php rename application/models/beheer/{User.php => User_model.php} (91%) create mode 100644 application/models/eloquent/Page.php create mode 100644 application/models/eloquent/User.php diff --git a/application/config/autoload.php b/application/config/autoload.php index 34afbb7..991a345 100755 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -132,4 +132,4 @@ $autoload['language'] = array(); | | $autoload['model'] = array('first_model' => 'first'); */ -$autoload['model'] = ['beheer/User']; +$autoload['model'] = ['beheer/User_model']; diff --git a/application/config/config.php b/application/config/config.php index 73cdd4e..8f1906d 100755 --- a/application/config/config.php +++ b/application/config/config.php @@ -100,7 +100,7 @@ $config['charset'] = 'UTF-8'; | setting this variable to TRUE (boolean). See the user guide for details. | */ -$config['enable_hooks'] = FALSE; +$config['enable_hooks'] = TRUE; /* |-------------------------------------------------------------------------- diff --git a/application/config/hooks.php b/application/config/hooks.php index a8f38a5..c291608 100755 --- a/application/config/hooks.php +++ b/application/config/hooks.php @@ -11,3 +11,13 @@ defined('BASEPATH') OR exit('No direct script access allowed'); | https://codeigniter.com/user_guide/general/hooks.html | */ + +$hook['post_controller_constructor'][] = [ + 'class' => 'EloquentHook', + 'function' => 'bootEloquent', + 'filename' => 'EloquentHook.php', + 'filepath' => 'hooks' +]; + + + diff --git a/application/controllers/Login.php b/application/controllers/Login.php index 0ea8006..a3d257f 100644 --- a/application/controllers/Login.php +++ b/application/controllers/Login.php @@ -21,7 +21,7 @@ class Login extends MY_Controller { $username = $this->clean_input( 'username' ); $password = $this->clean_input( 'password' ); - $user = $this->User->getUserByUsername( $username ); + $user = $this->User_model->getUserByUsername( $username ); if ( ! $user ) return $this->json( FALSE ); diff --git a/application/controllers/Main.php b/application/controllers/Main.php index 8767f55..b2627a4 100755 --- a/application/controllers/Main.php +++ b/application/controllers/Main.php @@ -12,7 +12,11 @@ class Main extends MY_Controller { { if( AUTHENTICATED == FALSE ) return $this->showLoginPage(); - $this->load->view('main_view'); + $pages = Page::getPages(); + + $this->json($pages->toArray()); + + //$this->load->view('main_view'); } private function showLoginPage() diff --git a/application/hooks/EloquentHook.php b/application/hooks/EloquentHook.php new file mode 100644 index 0000000..431771a --- /dev/null +++ b/application/hooks/EloquentHook.php @@ -0,0 +1,60 @@ +instance =& get_instance(); + } + + /** + * Loads database + */ + private function loadDatabase() { + $this->instance->load->database(); + } + + /** + * Returns the instance of the db + * @return object + */ + private function getDB() { + return $this->instance->db; + } + + public function bootEloquent() { + + $this->setInstance(); + + $this->loadDatabase(); + + $config = $this->getDB(); + + $capsule = new Capsule; + + $capsule->addConnection([ + 'driver' => 'mysql', + 'host' => $config->hostname, + 'database' => $config->database, + 'username' => $config->username, + 'password' => $config->password, + 'charset' => $config->char_set, + 'collation' => $config->dbcollat, + 'prefix' => $config->dbprefix, + ]); + + $capsule->setAsGlobal(); + $capsule->bootEloquent(); + } + +} \ No newline at end of file diff --git a/application/models/beheer/User.php b/application/models/beheer/User_model.php similarity index 91% rename from application/models/beheer/User.php rename to application/models/beheer/User_model.php index bf38e31..151fbb9 100644 --- a/application/models/beheer/User.php +++ b/application/models/beheer/User_model.php @@ -6,7 +6,7 @@ * Time: 19:37 */ -class User extends MY_Model { +class User_model extends MY_Model { protected $tablename = "users"; diff --git a/application/models/eloquent/Page.php b/application/models/eloquent/Page.php new file mode 100644 index 0000000..e13a9dd --- /dev/null +++ b/application/models/eloquent/Page.php @@ -0,0 +1,31 @@ +hasMany( 'Page' ); + } + + + public static function getPages() + { + $pages = Page::whereRaw( 'id = page_id' ) + ->with( 'subpages' ) + ->get(); + + return $pages; + } + + +} \ No newline at end of file diff --git a/application/models/eloquent/User.php b/application/models/eloquent/User.php new file mode 100644 index 0000000..30bea4f --- /dev/null +++ b/application/models/eloquent/User.php @@ -0,0 +1,18 @@ +=5.3.7", - "vlucas/phpdotenv": "^2.4" + "vlucas/phpdotenv": "^2.4", + "illuminate/database": "5.1.16" + }, + "autoload": { + "classmap": [ + "application/models/eloquent" + ] }, "suggest": { "paragonie/random_compat": "Provides better randomness in PHP 5.x"