Skip to content

KissHMVC PHP Framework - A simple easy to learn PHP framework designed with the intention of learning and teaching. I created it because I was curious how frameworks work and wondered whether I could create one myself. Still a work in progress...

License

nomadevs/Kiss-HMVC

Repository files navigation

Logo K.I.S.S.H.M.V.C. (Keep-It-Simple-Stupid-Hierarchical-Model-View-Controller) - CodeIgniter's illegitimate lovechild ;-)

Powered by Coffee and Solar! Literally, I'm living off a battery and caffeine to make this possible.

License PHP Version

UPDATES - I've fixed a lot of problems I didn't even know existed. I guess that's a sign my web development skills are improving. Still waiting for more reliable internet to upload all the changes. I'm literally chipping away to get everything working. [01/01/21]

UPDATES - I've made some progress on my framework. A lot of improvements and fixed bugs. Still has some, however. I can't update the repository until I renew my data plan very soon. Planning on switching carriers for truly unlimited internet, but I digress. I'm also integrating SQLite3 in to the Database class as a "driver" for those interested in Dev testing. [12/22/20]

UPDATES - I've begun working on this project again after a year. I recommend not using yet until a more stable version is released. Not everything works at the moment. Having to restudy my code again but it's coming back to me. [12/20/20]

KissHMVC is modeled after the CodeIgniter framework which I personally like to use because it's simple to understand and isn't bloated like other frameworks. KissHMVC follows the same philosophy! KissHMVC uses some modified code from the CodeIgniter framework (v3.1.11) and includes any necessary attribution (i.e. copyrights, and licenses). It's also a mashup of my own code, code from around the web, studying everything to the best of my knowledge, a crap load of trial & error, and this is the result! If you trust my code feel free to use it but make sure to keep all licenses and copyrights intact if you plan to make derivatives of it. Especially, since my framework is also a derivative of CodeIgniter and shares the same license! If you do want to make derivatives of my framework, please let me know so I can mention you on my website! If you're worried about security be sure to look through my code and make any necessary changes to secure it yourself, if you spot anything I may have missed. I have covered security for the most part so it's fairly secure. However, don't rely on that! My code should be simple enough to understand. I believe code should be simple to read in case you come back to it years later but also for new coders that are still learning themselves.

I'm not claiming to be a PHP expert and like any framework there could be vulnerabilities as I've stated above. You've been warned! I'm also not entirely sure how stable my framework is or how well it will hold up to massive applications. But since some of the code used comes from CodeIgniter you should have no problems. I'd be interested to hear back from some of you that have attempted. The great thing about my framework is that it's the best of both worlds. You can use normal MVC which has the added bonus of loading Controllers inside other controllers, unlike CodeIgniter, which only lets you load Models and Views in controllers. Or, you have the option to use Modules ( another feature CI doesn't offer straight out of the box ) to organize your application.

If you're familiar with CodeIgniter then KissHMVC will be easy to learn. I'm not trying to compete with other frameworks although that may be a possibility in the future depending on any contributers that would like to help me make that a reality and also my ability to make progress on it myself.

Furthermore, I'm still working out the bugs, fine tuning things, and studying CodeIgniter's code to get a general idea of how it works so I can make the necessary changes in my framework minus things I don't want in my framework. So keep checking back often for updates as I continue to make progress! If you would like to contribute, give me coding tips, please contact me here. I look forward to hearing from you and happy coding! =^)

Table of Contents

Requirements

  • Still testing compatability with 5.6 - 7.1+
  • SQLite3 (not fully working yet)

Documentation

A work in progress; Simply extract the zip folder you downloaded from this repository in to the root folder of your webhost or your development server and navigate to: https://<your_website>/user_guide OR http://<your_localhost>/user_guide in your browser to view the documentation. However, documenation is lacking at the moment. I'll be making progress on it as soon as possible.

Installation

Edit your .htaccess file and make sure mod_rewrite is enabled.

Htaccess Template

## I've placed an HTACCESS file for your convenience at the root of the zip file.
## If you plan on running your app from a folder then set that below in your HTACCESS file. If not,
## and you extracted the contents to the root, then change RewriteBase to a forward slash (/).
RewriteEngine On
Options -Indexes
RewriteBase /<your_folder>/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Then, in your routes.php file (application/config/routes.php) set your default controller.

// application/config/routes.php
$route['default_controller']   = '<your_controller>/index'; // Alternatively, you can specify a module here as well

Next, in your config.php file (application/config/config.php) set your base_url to:

$config['base_url']  = 'http://<your_website>/'; // (With folder) http://<your_website>/<your_folder>/

Finally, open up your browser (make sure your server software is running), and navigate to: http://<your_website>/ and you should be presented with your newly created module or controller.

Features

  • You can use default MVC or make use of the caked in Modules.
  • You can call controllers from within other controllers.

Planned / Ideas

  • Form validation works now! I'll be uploading the changes in the next day or so. Some minor bugs still, however. This was a pain in the butt to get working.
  • Include JS and CSS by calling get_scripts(); and get_styles(); (Haven't uploaded the changes yet but they work.)
  • Ajax CRUD Library Class
  • Upload and Search Library Classes
  • Model written in to the core;The user will still be able to create models that overwrite the core Model.
  • And more...?!

Tutorials (Example Usage)

Create a Page Controller

<?php
// application\controllers\Pages.php
defined('BASEPATH') OR exit('Direct script access not allowed');

class Pages extends KISS_Controller
{
  
  public function __construct()
  {
    parent::__construct();
  }

  public function index( $page = 'home' ) // Don't forget to create a view file called 'home'!
  { 
    $data['page_title'] = ucfirst($page);
    $this->load->view($page, $data);
  }

}

Then, in your routes.php file (application/config/routes.php) set to this:

<?php
// application\config\routes.php
defined('BASEPATH') OR exit('Direct script access not allowed');

$route['default_controller']   = 'Pages/index';
$route['(:any)']               = 'Pages/index/$1';

Loading a Module View

NOTE: This may change in the future (i.e. you would load views the same way as default MVC).

Kiss-HMVC loads module views slightly different to avoid conflicts. To load a module view you simply specify the module the view is contained in followed by the view you're trying to load. For example:

$this->load->view('<module_folder>/<view_file>');

Simple and Secure Way of Including CSS and JS

NOTE: I haven't uploaded the changes to github, so this doesn't work just yet. But the functions do work in my local repository and should work as soon as I push them to github.

I've created two global helper functions. Simply call: get_scripts(); or get_styles(); inside of your view files. Once you've done that an error message will appear requesting you to create some folders and files. By default if no parameters are passed specifying a custom stylesheet or script, the framework will try to find it for you. It will search for a css or js file containing the same name as the module it exists in. If you'd like to specify a script or stylesheet with a different name pass it in, for example: get_styles('my_styles'); and create it in the same css folder.

Create a Blog Module

<?php
// application\modules\Blog\controllers\Blog.php
defined('BASEPATH') OR exit('Direct script access not allowed');

class Blog extends KISS_Controller 
{
  public function __construct()
  {
    parent::__construct();
    // Set local timezone
    date_default_timezone_set('America/Vancouver');

    /** 
     * This is just an example, you don't have to load the configuration like 
     *  I am here.
     * You can autoload the database in (application/config/autoload.php) 
     * - OR - 
     * call it here, for example: $this->load->library('database'); (minus the 
     * config settings) 
     * 
     */
     $this->load->library('database', array(
          'db_host' => 'localhost',
          'db_user' => 'root',
          'db_pass' => '',
          'db_name' => 'YOUR_DATABASE_NAME'
    ));
    // Load text helper so we can use word_limiter() for posts excerpts in blog feed
    $this->load->helper('text');
    $this->load->model('Blog/Blog_model');
    $this->model = $this->Blog_model;
    $this->load->library('Custom_Dates'); /* I created a custom library that
    converts timestamps to human readable dates (application/library/Custom_Dates.php) */
  }

  public function index()
  {
    $data['posts'] = $this->model->get_posts()->result_array();
    $data['site_title'] = 'Blog';
    $data['page_title'] = 'Blog';
    $data['body_class'] = 'blog-page';
    $this->load->view('Blog/header',$data);
    $this->load->view('Blog/posts',$data);
    $this->load->view('Blog/footer',$data);
  }

  public function post($slug = NULL)
  {
    $data['post'] = $this->model->get_post($slug);
    $data['site_title'] = $data['post']['post_title'];
    $data['page_title'] = $data['post']['post_title'];
    $data['body_class'] = 'post-page';
    $this->load->view('Blog/header',$data);
    $this->load->view('Blog/post',$data);
    $this->load->view('Blog/footer',$data);
  }

Then, in your routes.php file (application/config/routes.php) set to this:

<?php
// application\config\routes.php
defined('BASEPATH') OR exit('Direct script access not allowed');

$route['blog']             = 'Blog/index';
$route['blog/post/(:any)'] = 'Blog/post/$1';

Next, create a model file called Blog_model.php including a corresponding table in your database called 'posts' (example below).

Create a file called 'posts.sql'

# Copy & paste these contents (minus these comments) in to the file you just created, save it, and import
# in to your database by selecting your database name in phpMyAdmin (http://localhost/phpmyadmin), click 
# 'import' tab, click 'choose file' button, browse for your sql file, 
# select it by left-clicking once, and click 'open' button below in dialog
# box. Once that windows closes out, scroll down to the bottom of the current
# screen and click 'Go' button to submit changes.

CREATE TABLE `posts` (
  `id` int(11) NOT NULL,
  `post_title` varchar(255) NOT NULL,
  `post_body` text NOT NULL,
  `post_date` int(11) NOT NULL,
  `post_slug` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `posts`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `posts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT; 
<?php
// application\modules\Blog\models\Blog_model.php
defined('BASEPATH') OR exit('Direct script access not allowed');

class Blog_model extends KISS_Model
{
  public function __construct()
  {
    parent::__construct();
    $this->table = 'posts';
  } 

  public function get_posts() 
  {
    return $this->db->query("SELECT * FROM $this->table ORDER BY id DESC");
  }  

  public function get_post( $slug )
  {
    return $this->db->query("SELECT * FROM $this->table WHERE post_slug = '$slug' ORDER BY post_date DESC")->row_array();
  }
}

Lastly, create two files, one called post.php and the other called posts.php and put them in your views folder.

post.php (Displays a single post)

<?php 
  // application\modules\Blog\views\post.php
  defined('BASEPATH') OR exit('Direct script access not allowed'); 
?>

  <h2><?php echo $post['post_title']; ?></h2>
  <date>
    <?php echo $this->Custom_Dates->get_nice_date($post['post_date'],'full_date_time'); ?>
  </date>
  <p><?php echo $post['post_body']; ?></p>

posts.php (Displays all posts)

<?php 
  // application\modules\Blog\views\posts.php
  defined('BASEPATH') OR exit('Direct script access not allowed'); 
?>
<h1>Blog</h1>
<?php foreach( $posts as $post ) : ?>
  <h2><a href="<?php echo base_url("blog/post/$post[post_slug]"); ?>"><?php echo $post['post_title']; ?></a></h2>
  <date>
    <?php echo $this->Custom_Dates->get_nice_date($post['post_date'],'full_date_time'); ?>
  </date>
  <p><?php echo word_limiter($post['post_body'],25,' [...]'); ?></p>
  <hr />
<?php endforeach; ?>

Create a Template Module

Create a folder called Templates in the Modules folder (application/modules), and include 3 folders in that called (controllers, models, views). Or, simply copy the COPYTHIS folder which I've included to make things easier. Create a file (example below) called Templates.php in the controllers folder. Then, put your header.php and footer.php files in the views folder.

Create a folder called Dashboard in the Modules folder, and include 3 folders in that called (controllers, models, views). Or, just like above, simply copy the COPYTHIS folder. Create a file (example below) called Dashboard.php in the controllers folder and finally create another file in your Views folder called dashboard.php.

Templates.php

<?php
// application\modules\Templates\controllers\Templates.php
defined('BASEPATH') OR exit('Direct script access not allowed');

class Templates extends KISS_Controller 
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index(){}
  
  public function dashboard($data)
  {
    $this->load->view('Templates/header',$data);
    $this->load->view($data['view_module'].'/'$data['view_file'],$data);
    $this->load->view('Templates/header',$data);
  }

}

header.php

<?php
// application\modules\Templates\views\header.php
defined('BASEPATH') OR exit('Direct script access not allowed');
?>
<!DOCTYPE html>
<html lang='en'>
  <head>
    <title><?php echo $site_title; ?></title>
  </head>
  <body class="<?php echo $body_class; ?>">

footer.php

<?php
// application\modules\Templates\views\footer.php
defined('BASEPATH') OR exit('Direct script access not allowed');
?>

    <footer>&copy; Copyright <?php echo date('Y'); ?></footer>
  </body>
</html>

Dashboard.php

<?php
// application\modules\Dashboard\controllers\Dashboard.php
defined('BASEPATH') OR exit('Direct script access not allowed');

class Dashboard extends KISS_Controller 
{
  public function __construct()
  {
    parent::__construct();
  }
  
  public function index()
  {
    $data['site_title']  = 'Dashboard';
    $data['page_title']  = 'Dashboard';
    $data['body_class']  = 'dashboard-page';
    $data['view_file']   = 'dashboard';
    $data['view_module'] = 'Dashboard';
    $this->load->module('Templates/dashboard',$data);
  }

}

dashboard.php

<?php
// application\modules\Dashboard\views\dashboard.php
defined('BASEPATH') OR exit('Direct script access not allowed');
?>
<h2><?php echo $page_title; ?></h2>

Result

<!DOCTYPE html>
<html lang='en'>
  <head>
    <title>Dashboard</title>
  </head>
  <body class="dashboard-page">

    <h2>Dashboard</h2>

    <footer>© Copyright 2020</footer>
  </body>
</html>

Changelog

You can view the CHANGELOG file for information.

Contribute

You can view the CONTRIBUTE file for information.

License

KissHMVC is licensed under an MIT License. You can view the LICENSE file for information.

Credits

Inspired by CodIigniter, StackOverflow, YouTube, and Other Online Resources.

Contact

Feel free to get in touch with me via my website: https://mywebfolio.me and my email: philosaphylas@gmail.com!

About

KissHMVC PHP Framework - A simple easy to learn PHP framework designed with the intention of learning and teaching. I created it because I was curious how frameworks work and wondered whether I could create one myself. Still a work in progress...

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published