Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Usually I have a file named Autoloader.php but as i wrote this is a raw example...
- spl_autoload_register(function($className){
- $className = str_replace('\', DIRECTORY_SEPARATOR, $className);
- include_once $className.'.php';
- });
- $model = new Model;
- $controller = new Controller($model, new TemplateLoader('templates'));
- $tpl = new TemplateLoader('templates/');
- if(isset($_SERVER['PATH_INFO'])){
- $path = $_SERVER['PATH_INFO'];
- $path_split = explode('/', ltrim($path));
- }else{
- $path_split = '/';
- }
- if($path_split === '/'){
- echo $controller->index();
- }
- else{
- $req_ctr = $path_split[1].'Controller';
- $req_action = $path_split[2];
- $controller = new $req_ctr($tpl);
- echo $controller->$req_action();
- }
- ?>
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteRule ^(.+)$ index.php/$1 [L]
- <?php
- class userController{
- public function __construct(TemplateLoader $TemplateLoader){
- $this->tpl = $TemplateLoader;
- }
- public function login(){
- return $this->tpl->renderTemplate('login');
- }
- public function doLogin(){
- $this->user = $_POST['username'];
- $this->password = $_POST['password'];
- if($this->user === 'demo' && $this->password === 'demo'){
- return header('Location: dashboard');
- }
- }
- public function dashboard(){
- $data = array('username'=> $this->user,'message'=> 'Login success');
- return $this->tpl->renderTemplate('dashboard', $data);
- }
- }
- ?>
- <?php
- class Model{
- public function __construct(){
- }
- }
- ?>
- <?php
- class View{
- public function __construct(TemplateLoader $tpl){
- $this->tpl = $tpl;
- }
- }
- ?>
- <?php
- class Controller{
- public function __construct($Model,TemplateLoader $TemplateLoader){
- $this->model = $Model;
- $this->tpl = $TemplateLoader;
- }
- public function index(){
- return 'Index';
- }
- }
- ?>
- <html>
- <body>
- <h1>Test login MVC</h1>
- <form method="POST" action="doLogin">
- <input type="text" name="username" />
- <br>
- <input type="password" name="password" />
- <br>
- <input type="submit" value="LOGIN"/>
- </form>
- </body>
- </html>
- <html>
- <body>
- <h1>Test MVC Dashboard</h1>
- <p>Ciao <?php echo $username; ?></p>
- <p><?php echo $message; ?></p>
- </body>
- </html>
Add Comment
Please, Sign In to add comment