├── .gitignore ├── README.md ├── app ├── controllers │ ├── PagesController.php │ └── UsersController.php ├── models │ ├── Project.php │ └── User.php ├── routes.php └── views │ ├── about.view.php │ ├── contact.view.php │ ├── error.view.php │ ├── index.view.php │ ├── partials │ ├── footer.php │ ├── header.php │ └── nav.php │ ├── user.view.php │ └── users.view.php ├── composer.json ├── config.php.example ├── core ├── App.php ├── Request.php ├── Router.php ├── bootstrap.php ├── database │ ├── Connection.php │ ├── Model.php │ └── QueryBuilder.php ├── helpers.php └── logger │ ├── LogToDatabase.php │ ├── LogToFile.php │ └── Logger.php ├── logs └── .gitignore ├── people.sql ├── public ├── .htaccess ├── css │ ├── bootstrap.min.css │ └── main.css ├── favicon.ico ├── img │ └── logo.png ├── index.php └── js │ ├── bootstrap.min.js │ ├── dark-toggle.js │ ├── jquery.min.js │ ├── main.js │ └── popper.min.js └── tests ├── Feature └── FeatureTest.php ├── TestCase.php └── Unit └── UnitTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/** 3 | config.php 4 | .idea/** 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple-PHP-MVC-Framework 2 | This is a simple PHP MVC framework. Made in 2017, it uses Composer and is designed to be very simple to use. Inspired by Laravel. 3 | 4 | # Requirements 5 | * PHP >= 7.3.0 6 | * MySQL >= 5.6.10 7 | * Composer 8 | 9 | # How to Install 10 | Note: It is recommended that you install LAMP with my LAMP install script to ensure that you have all of the requirements. 11 | 12 | My LAMP install script is located at 13 | 14 | https://github.com/kenstuddy/Deploy-LAMP 15 | 16 | Clone the repository 17 | 18 | ``` 19 | git clone https://github.com/kenstuddy/Simple-PHP-MVC-Framework 20 | ``` 21 | 22 | Change to the repository directory 23 | 24 | ``` 25 | cd Simple-PHP-MVC-Framework 26 | ``` 27 | 28 | Run composer to install any PHP dependencies 29 | 30 | ``` 31 | composer install 32 | ``` 33 | 34 | Change to the public directory 35 | 36 | ``` 37 | cd public 38 | ``` 39 | 40 | Start the PHP server (0.0.0.0 is the default route, this makes PHP listen on all IPv4 interfaces) 41 | 42 | ``` 43 | php -S 0.0.0.0:8000 44 | ``` 45 | 46 | Visit the IP address (127.0.0.1 if you are running Linux natively, or the IP address of your VM/VPS/etc) http://127.0.0.1:8000 in your web browser. 47 | 48 | To run the included unit tests, make sure you are still in the public directory, and then type the following command 49 | 50 | ``` 51 | ../vendor/bin/phpunit ../tests 52 | ``` 53 | -------------------------------------------------------------------------------- /app/controllers/PagesController.php: -------------------------------------------------------------------------------- 1 | 40 | -------------------------------------------------------------------------------- /app/controllers/UsersController.php: -------------------------------------------------------------------------------- 1 | count(); 22 | $paginationConfig = App::Config()['pagination']; 23 | $limit = $paginationConfig['per_page'] ?? 5; 24 | $page = $vars['page'] ?? 1; 25 | $offset = ($page - 1) * $limit; 26 | $users = $user->where([['user_id', '>', '0']], $limit, $offset)->get(); 27 | return view('users', compact('users', 'count', 'page', 'limit')); 28 | } 29 | 30 | /* 31 | * This function selects a the user from the users database and then grabs the user view to display them. 32 | */ 33 | public function show($vars) 34 | { 35 | //Here we use the Query Builder to get the user: 36 | /*$user = App::DB()->selectAllWhere('users', [ 37 | ['user_id', '=', $vars['id']], 38 | ]); 39 | */ 40 | 41 | //Here we use the ORM to get the user: 42 | $user = new User(); 43 | $foundUser = $user->find($vars['id']); 44 | $user = $foundUser ? $foundUser->get() : []; 45 | 46 | if (empty($user)) { 47 | redirect('users'); 48 | } 49 | return view('user', compact('user')); 50 | } 51 | 52 | /* 53 | * This function inserts a new user into our database using array notation. 54 | */ 55 | public function store() 56 | { 57 | App::DB()->insert('users', [ 58 | 'name' => $_POST['name'] 59 | ]); 60 | $paginationConfig = App::Config()['pagination']; 61 | if ($paginationConfig['show_latest_page_on_add']) { 62 | $totalRecords = App::DB()->count('users'); 63 | $recordsPerPage = $paginationConfig['per_page'] ?? 5; 64 | $lastPage = ceil($totalRecords / $recordsPerPage); 65 | return redirect('users/' . $lastPage); 66 | } else { 67 | return redirect('users'); 68 | } 69 | } 70 | 71 | /* 72 | * This function updates a user from our database using array notation. 73 | */ 74 | public function update($vars) 75 | { 76 | App::DB()->updateWhere('users', [ 77 | 'name' => $_POST['name'] 78 | ], [ 79 | ['user_id', '=', $vars['id']] 80 | ]); 81 | return redirect('user/' . $vars['id']); 82 | } 83 | 84 | /* 85 | * This function deletes a user from our database. 86 | */ 87 | public function delete($vars) 88 | { 89 | App::DB()->deleteWhere('users', [ 90 | ['user_id', '=', $vars['id']] 91 | ]); 92 | $paginationConfig = App::Config()['pagination']; 93 | if ($paginationConfig['show_latest_page_on_delete']) { 94 | $currentPage = $_GET['page'] ?? 1; 95 | $recordsPerPage = $paginationConfig['per_page'] ?? 5; 96 | $totalRecordsAfterDeletion = App::DB()->count('users'); 97 | $lastPageAfterDeletion = max(ceil($totalRecordsAfterDeletion / $recordsPerPage), 1); 98 | if ($currentPage > $lastPageAfterDeletion) { 99 | $redirectPage = $lastPageAfterDeletion; 100 | } else { 101 | $redirectPage = $currentPage; 102 | } 103 | return redirect('users/' . $redirectPage); 104 | } else { 105 | return redirect('users'); 106 | } 107 | } 108 | } 109 | 110 | ?> 111 | -------------------------------------------------------------------------------- /app/models/Project.php: -------------------------------------------------------------------------------- 1 | 20 | -------------------------------------------------------------------------------- /app/models/User.php: -------------------------------------------------------------------------------- 1 | 21 | -------------------------------------------------------------------------------- /app/routes.php: -------------------------------------------------------------------------------- 1 | getArray([ 6 | '' => 'PagesController@home', 7 | 'about' => 'PagesController@about', 8 | 'contact' => 'PagesController@contact', 9 | 'users' => 'UsersController@index', 10 | 'users/{page}' => 'UsersController@index', 11 | 'user/{id}' => 'UsersController@show', 12 | 'user/delete/{id}' => 'UsersController@delete' 13 | ]); 14 | $router->postArray([ 15 | 'users' => 'UsersController@store', 16 | 'user/update/{id}' => 'UsersController@update', 17 | ]); 18 | } 19 | else { 20 | $router->get('', 'PagesController@home'); 21 | $router->get('about', 'PagesController@about'); 22 | $router->get('contact', 'PagesController@contact'); 23 | 24 | $router->get('users', 'UsersController@index'); 25 | $router->get('users/{page}', 'UsersController@index'); 26 | $router->get('user/{id}', 'UsersController@show'); 27 | $router->get('user/delete/{id}', 'UsersController@delete'); 28 | $router->post('users', 'UsersController@store'); 29 | $router->post('user/update/{id}', 'UsersController@update'); 30 | } 31 | 32 | ?> 33 | -------------------------------------------------------------------------------- /app/views/about.view.php: -------------------------------------------------------------------------------- 1 | 2 |
Simple PHP MVC Framework is a framework created by Ken Studdy in 2017. It is inspired by Laravel, and has a query builder, ORM, pagination, a lightweight templating system, and more. You can find the source code for it here.
4 | 5 | -------------------------------------------------------------------------------- /app/views/contact.view.php: -------------------------------------------------------------------------------- 1 | 2 |If you'd like to contact , please feel free to use my contact form on or email me at
4 | 5 | -------------------------------------------------------------------------------- /app/views/error.view.php: -------------------------------------------------------------------------------- 1 | 2 |You have encountered an error. Please click here to go to the home page.
4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/views/index.view.php: -------------------------------------------------------------------------------- 1 | 2 |Welcome to the Simple PHP MVC Framework! Simple PHP MVC Framework is a fast PHP MVC framework designed to allow for easy creation of powerful websites and web applications. It is inspired by Laravel, and is designed to be easy to use.
4 |