├── .gitignore ├── view └── home.php ├── index.html ├── config └── database.php ├── model ├── TestModel.php └── UserModel.php ├── core ├── CJ_Controller.php ├── database │ └── CJ_Connection.php └── CJ_Model.php ├── controller ├── Test.php └── User.php ├── LICENSE ├── test.php ├── index.php ├── database_files └── init.sql └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /view/home.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | home 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /config/database.php: -------------------------------------------------------------------------------- 1 | 'localhost', 4 | 'username' => 'root', 5 | 'password' => 'root', 6 | 'dbname' => 'forum' 7 | 8 | ); 9 | 10 | ?> -------------------------------------------------------------------------------- /model/TestModel.php: -------------------------------------------------------------------------------- 1 | "; 9 | } 10 | 11 | function sayHello($name){ 12 | echo "Welcome to ". $name; 13 | } 14 | 15 | 16 | } 17 | 18 | ?> -------------------------------------------------------------------------------- /model/UserModel.php: -------------------------------------------------------------------------------- 1 | read('users',array('*'),null); 13 | } 14 | 15 | function get($id){ 16 | 17 | return $this->read('users', array('*'), array('id'=>$id)); 18 | } 19 | } 20 | ?> -------------------------------------------------------------------------------- /core/CJ_Controller.php: -------------------------------------------------------------------------------- 1 | $vvalue) { 16 | 17 | $$vname = $vvalue; 18 | } 19 | require_once(__DIR__.'/../view/'.$view.'.php'); 20 | 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | 28 | ?> -------------------------------------------------------------------------------- /core/database/CJ_Connection.php: -------------------------------------------------------------------------------- 1 | db_params = $db_params; 9 | } 10 | 11 | 12 | function getConnection(){ 13 | $conn = new mysqli($this->db_params['servername'],$this->db_params['username'],$this->db_params['password'],$this->db_params['dbname']); 14 | if($conn->connect_error){ 15 | die("Connection Faild: ". $conn->connect_error); 16 | } 17 | return $conn; 18 | } 19 | 20 | } 21 | 22 | 23 | ?> -------------------------------------------------------------------------------- /controller/Test.php: -------------------------------------------------------------------------------- 1 | "; 9 | $this->test_model = new TestModel(); 10 | } 11 | 12 | 13 | function test_get(){ 14 | 15 | $this->load_view('home', array('content'=>'

This content is sent from controller

')); 16 | } 17 | 18 | function hello_get(...$args){ 19 | 20 | $this->test_model->sayHello('CJ_MODEL'); 21 | } 22 | 23 | 24 | } 25 | 26 | 27 | 28 | 29 | 30 | ?> -------------------------------------------------------------------------------- /controller/User.php: -------------------------------------------------------------------------------- 1 | "; 10 | $this->model=new UserModel(); 11 | 12 | } 13 | 14 | function hello_get(){ 15 | echo "Hello, World!"; 16 | } 17 | 18 | 19 | //http://localhost/CJ-MVC/index.php/User/getAll/ 20 | function getAll_get(){ 21 | $result = $this->model->get_all(); 22 | print_r($result); 23 | echo json_encode($result); 24 | } 25 | 26 | 27 | //http://localhost/CJ-MVC/index.php/User/get/2/ 28 | function get_get($id){ 29 | 30 | $result = $this->model->get($id); 31 | echo json_encode($result); 32 | 33 | 34 | } 35 | 36 | } 37 | 38 | 39 | ?> -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Chaitya Shah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | $value){ 14 | if($value == 'index.php'){ 15 | if($key == count($uri) - 1 ) return -1; 16 | return $key+1; 17 | } 18 | } 19 | } 20 | 21 | $function_name = $result[getArgumentStart($result)]; 22 | 23 | $arr = get_class_methods ( 'TEST' ); 24 | 25 | print_r($arr); 26 | 27 | //print_r($arr['user'][1]()); 28 | 29 | function hello($a, $b){ 30 | 31 | echo "Hello, World"; 32 | return true; 33 | }; 34 | 35 | 36 | 37 | class TEST{ 38 | 39 | function __construct(){ 40 | echo "TEST CREAITED"; 41 | } 42 | 43 | function asdf($a, $b){ 44 | echo "
"."Inside test
"; 45 | } 46 | } 47 | 48 | 49 | 50 | //$r = new TEST(); 51 | 52 | 53 | call_user_func_array(array(new TEST, 'asdf'), array('argument1', 'argument2')); 54 | 55 | 56 | 57 | 58 | function hello12(){ 59 | 60 | echo "Hello, World"; 61 | 62 | }; 63 | 64 | //$fun(); 65 | // $$function_name; 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | $value){ 19 | if($value == 'index.php'){ 20 | if($key == count($uri) - 1 ) return -1; 21 | return $key+1; 22 | } 23 | } 24 | } 25 | 26 | 27 | function main(){ 28 | 29 | $uri = parse_url($_SERVER['REQUEST_URI']); 30 | 31 | //new php version have issue with split so using explode instead 32 | $parameters = explode('/', $uri['path']); 33 | // get query using $uri['query'] // TODO 34 | $start = getArgumentStart($parameters); 35 | if($start != -1){ 36 | $controller_name = $parameters[$start]; 37 | 38 | $function_name = $parameters[$start+1] . "_" . strtolower($_SERVER['REQUEST_METHOD']); 39 | $start+=2; 40 | $args = array(); 41 | for(;$start < count($parameters); $start++){ 42 | array_push($args, $parameters[$start]); 43 | } 44 | 45 | call_user_func_array(array(new $controller_name, $function_name), $args); 46 | 47 | 48 | }else{ 49 | echo "404 not found"; 50 | } 51 | 52 | 53 | } 54 | 55 | main(); 56 | 57 | ?> -------------------------------------------------------------------------------- /database_files/init.sql: -------------------------------------------------------------------------------- 1 | 2 | -- drops the database ,beware 3 | 4 | -- DROP DATABASE forum ; 5 | 6 | CREATE DATABASE forum; 7 | 8 | USE forum; 9 | 10 | CREATE TABLE `users` ( 11 | `id` int(11) NOT NULL AUTO_INCREMENT, 12 | `username` varchar(255) NOT NULL, 13 | `email` varchar(255) NOT NULL, 14 | `password` text NOT NULL, 15 | `upvotes` int(11) NOT NULL, 16 | PRIMARY KEY (`id`) 17 | ); 18 | 19 | CREATE TABLE `questions` ( 20 | `id` int(11) NOT NULL AUTO_INCREMENT, 21 | `user_id` int(11) DEFAULT NULL, 22 | `what` text, 23 | `kabhi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 24 | PRIMARY KEY (`id`), 25 | KEY `user_id` (`user_id`), 26 | CONSTRAINT `questions_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) 27 | ); 28 | 29 | CREATE TABLE `answers` ( 30 | `id` int(11) NOT NULL AUTO_INCREMENT, 31 | `user_id` int(11) DEFAULT NULL, 32 | `question_id` int(11) DEFAULT NULL, 33 | `what` text, 34 | `upvotes` int(11) DEFAULT NULL, 35 | `kabhi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 36 | PRIMARY KEY (`id`), 37 | KEY `question_id` (`question_id`), 38 | KEY `user_id` (`user_id`), 39 | CONSTRAINT `answers_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`), 40 | CONSTRAINT `quesions_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) 41 | ); 42 | 43 | CREATE TABLE `upvote_audit` ( 44 | `answer_id` int(11) NOT NULL DEFAULT '0', 45 | `user_id` int(11) NOT NULL DEFAULT '0', 46 | PRIMARY KEY (`answer_id`,`user_id`), 47 | KEY `user_id` (`user_id`), 48 | CONSTRAINT `upvote_audit_ibfk_1` FOREIGN KEY (`answer_id`) REFERENCES `answers` (`id`), 49 | CONSTRAINT `upvote_audit_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) 50 | ); 51 | 52 | -- Tables data init 53 | -- users 54 | 55 | INSERT INTO `users` (`id`, `username`, `email`, `password`, `upvotes`) VALUES 56 | (1, 'jigar_wala', 'jigar.wala@forum.xyz', 'ne uh4ihncorn43uc8reucmjrnf84c8hr48m8rhjmshcnrs', 15), 57 | (2, 'chaitya62', 'chaitya.shah@forum.xyz', 'rehregr5b656bdtybr6ybetr7b6y', 100); 58 | 59 | -- quesions 60 | 61 | INSERT INTO `questions` (`id`, `user_id`, `what`, `kabhi`) VALUES 62 | (1, 1, 'what is angularJS ?', '2017-10-18 08:44:25'), 63 | (2, 2, 'wubba luba dub dub', '2017-10-18 08:44:25'); 64 | 65 | -- answers 66 | 67 | INSERT INTO `answers` (`id`, `user_id`, `question_id`, `what`, `upvotes`, `kabhi`) VALUES 68 | (1, 2, 1, 'angularJS is JS framework for frontend,\r\nthe project is maintained by google ,\r\nversions of angularJS include 1.x , 2.x , 4.x !!\r\nyes it skipped the version 3 for odd reason', NULL, '2017-10-18 08:47:12'), 69 | (2, 1, 2, 'is this related to the forum !\r\ni know you are rick and morty fan !\r\nbut man @-@\r\n\r\n', NULL, '2017-10-18 08:48:28'); 70 | 71 | -- upvote_audit 72 | 73 | INSERT INTO `upvote_audit` (`answer_id`, `user_id`) VALUES 74 | (1, 2), 75 | (2, 1); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | CJ-MVC-logo 4 |

5 | 6 |

An attempt to make a simple MVC from scratch in PHP

7 | 8 | 9 | ## Features 10 | 11 | - Handle get/post easily 12 | - Model Supports - Create, Read, Update, Delete and where clause. 13 | - Easlly load views from controller 14 | - Light Weight 15 | - Doesn't contain fancy features that you don't use anyways. 16 | 17 | 18 | ## How to use 19 | 20 | ### Setup 21 | Just clone this repository 22 | ```sh 23 | 24 | git clone https://github.com/Chaitya62/CJ-MVC.git 25 | 26 | ``` 27 | **OR** 28 | 29 | Download it and place it in the `htdocs` equivalent of your server. 30 | 31 | Edit the `database.php` inside config folder to connect it to your sql database. 32 | 33 | 34 | 35 | ### How it works 36 | The MVC follows a [CodeIgniter](https://codeigniter.com/) like url structure. 37 | 38 | **For Example** 39 | In the following url 40 | http://localhost/CJ-MVC/index.php/User/get/2/ 41 | 42 | - `CJ-MVC` is the project name 43 | - `index.php` is compulsory for now as it is the entry point to the app 44 | - `User` is the name of the Controller 45 | - `get` is the function you want 46 | - rest is passed as arguments to the function 47 | 48 | **Note** 49 | As the function name is `get_get` and not `get` the \_get means that want this function to handle the get requests. 50 | 51 | ### Controller 52 | 53 | To create your own controller just copy the `Test.php` file in the controller directory and build upon it. 54 | 55 | `require_once(__DIR__.'/../model/TestModel.php');` replace this line with your model if you have any or remove it. 56 | also replace the constructor accordingly 57 | 58 | ### Model 59 | 60 | To create your own model copy the `TestModel.php` file in the models directory. 61 | For further examples on using model see the usage in `User.php` 62 | 63 | ### Views 64 | 65 | To create your own view just create simple php views and load it using `load_view` function in the controller 66 | 67 | ```php 68 | $this->load_view('', ) 69 | 70 | ``` 71 | 72 | That's it you are ready to roll and create the next best site. 73 | 74 | 75 | 76 | ## Sawaal 77 | 78 | An app created using this framework 79 | [demo](https://testemailkjsce.000webhostapp.com/Forum/frontend/dist/index.html#/) [github](https://github.com/Chaitya62/Forum) 80 | 81 | > For more details on how the framework works go through the given example files in `controller`, `model` and `view` folders. 82 | 83 | [Here](https://chaitya62.github.io/2018/04/29/Writing-your-own-MVC-from-Scratch-in-PHP.html) is a blog post explaining this in detail. 84 | 85 | 86 | ## Help Us 87 | 88 | This framework is in the very early stages of it's development. 89 | 90 | You can help me by posting issues. 91 | 92 | or write me at [chaitya.shah@somaiya.edu](mailto://chaitya.shah@somaiya.edu) 93 | 94 | 95 | ## Contributors 96 | 97 | [@Chaitya62](https://github.com/Chaitya62) 98 | 99 | [@JigarWala](https://github.com/jigarWala) 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /core/CJ_Model.php: -------------------------------------------------------------------------------- 1 | '; 9 | $db = new CJ_Connection(); 10 | $this->connection = $db->getConnection(); 11 | } 12 | 13 | function create($tableName,$insertWhat){ 14 | 15 | $sql='INSERT INTO '.$tableName.'('; 16 | foreach ($insertWhat as $key => $value) 17 | $sql .= $key.','; 18 | 19 | $sql=rtrim($sql,','); 20 | $sql.=')'; 21 | $sql.=' VALUES('; 22 | 23 | foreach ($insertWhat as $key => $value) 24 | $sql .= '\''.$value.'\','; 25 | 26 | $sql=rtrim($sql,','); 27 | $sql.=')'; 28 | 29 | $sql=$this->appendSemicolon($sql); 30 | echo $sql.'
'; 31 | 32 | $result = $this->connection->query($sql); 33 | if($result) 34 | return $result; 35 | else 36 | return 'Error at CJ_MODEL/create'; 37 | } 38 | 39 | /* 40 | ****tablename necessary ! 41 | $tableName => 'users' 42 | 43 | ****what to read 44 | use accorgingly if all read / read particular column 45 | $args => array('*') / array('username','email') 46 | 47 | ****optional arg (call with null argument to avoid warnings) 48 | //for where clause 49 | $whereArgs => array( 'username' => 'jigar_wala') 50 | */ 51 | 52 | function read($tableName,$args,$whereArgs){ 53 | 54 | $sql='SELECT '; 55 | 56 | foreach ($args as $key => $value) 57 | $sql.=$value.','; 58 | $sql=rtrim($sql,','); 59 | $sql.=' FROM '.$tableName; 60 | 61 | if($whereArgs) 62 | $sql= $this->where($sql,$whereArgs); 63 | 64 | $sql=$this->appendSemicolon($sql); 65 | echo $sql.'
'; 66 | $finale=array(); 67 | 68 | $result = $this->connection->query($sql); 69 | if($result){ 70 | while($row=mysqli_fetch_assoc($result)) 71 | array_push($finale,$row); 72 | return $finale; 73 | } 74 | else 75 | return 'Error at CJ_MODEL/read'; 76 | 77 | } 78 | function update($tableName,$whatToSet,$whereArgs){ 79 | $sql='UPDATE '.$tableName .' SET '; 80 | foreach ($whatToSet as $key => $value) 81 | $sql .= $key .'=\'' . $value . '\','; 82 | $sql=rtrim($sql,','); 83 | 84 | if($whereArgs) 85 | $sql= $this->where($sql,$whereArgs); 86 | $sql=$this->appendSemicolon($sql); 87 | echo $sql.'
'; 88 | $result = $this->connection->query($sql); 89 | 90 | if($result) 91 | return $result; 92 | else 93 | return 'Error at CJ_MODEL/update'; 94 | 95 | } 96 | function delete($tableName,$whereArgs){ 97 | $sql='DELETE FROM '.$tableName; 98 | 99 | if($whereArgs) 100 | $sql=$this->where($sql,$whereArgs); 101 | $sql=$this->appendSemicolon($sql); 102 | echo $sql.'
'; 103 | $result = $this->connection->query($sql); 104 | 105 | if($result) 106 | return $result; 107 | else 108 | return 'Error at CJ_MODEL/delete'; 109 | 110 | 111 | } 112 | 113 | function where($sql,$whereArgs){ 114 | $sql.=' WHERE '; 115 | 116 | foreach ($whereArgs as $key => $value) 117 | $sql.=$key.' = \''.$value.'\' AND '; 118 | $sql=rtrim($sql,'AND '); 119 | 120 | return $sql; 121 | } 122 | 123 | function appendSemicolon($sql){ 124 | if(substr($sql,-1)!=';') 125 | return $sql.' ;'; 126 | } 127 | } 128 | 129 | 130 | // $obj=new CJ_Model(); 131 | 132 | // $where['id']='3'; 133 | // $where['evw']=2; 134 | 135 | // $rows=call_user_func_array(array($obj, 'create'), array('questions',array('user_id'=>1,'what'=>'isko insert karna he?'))); 136 | // $rows=call_user_func_array(array($obj, 'read'), array('users',array('*'),null)); 137 | // $rows=call_user_func_array(array($obj, 'update'), array('questions',array('what'=>'is science related to math?'),$where)); 138 | // $rows=call_user_func_array(array($obj, 'delete'), array('questions',$where)); 139 | // echo '
'; 140 | // print_r($rows) 141 | // print_r(get_class_methods ( 'CJ_Model' )); 142 | 143 | 144 | ?> --------------------------------------------------------------------------------