Guest User

Untitled

a guest
Sep 1st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <?php
  2. // Usually I have a file named Autoloader.php but as i wrote this is a raw example...
  3. spl_autoload_register(function($className){
  4. $className = str_replace('\', DIRECTORY_SEPARATOR, $className);
  5. include_once $className.'.php';
  6. });
  7.  
  8. $model = new Model;
  9. $controller = new Controller($model, new TemplateLoader('templates'));
  10. $tpl = new TemplateLoader('templates/');
  11.  
  12. if(isset($_SERVER['PATH_INFO'])){
  13. $path = $_SERVER['PATH_INFO'];
  14.  
  15. $path_split = explode('/', ltrim($path));
  16. }else{
  17.  
  18. $path_split = '/';
  19. }
  20. if($path_split === '/'){
  21.  
  22. echo $controller->index();
  23.  
  24. }
  25. else{
  26. $req_ctr = $path_split[1].'Controller';
  27. $req_action = $path_split[2];
  28. $controller = new $req_ctr($tpl);
  29. echo $controller->$req_action();
  30. }
  31.  
  32. ?>
  33.  
  34. RewriteEngine On
  35. RewriteCond %{REQUEST_FILENAME} !-d
  36. RewriteCond %{REQUEST_FILENAME} !-f
  37. RewriteRule ^(.+)$ index.php/$1 [L]
  38.  
  39. <?php
  40.  
  41. class userController{
  42.  
  43. public function __construct(TemplateLoader $TemplateLoader){
  44. $this->tpl = $TemplateLoader;
  45. }
  46.  
  47. public function login(){
  48. return $this->tpl->renderTemplate('login');
  49. }
  50.  
  51. public function doLogin(){
  52. $this->user = $_POST['username'];
  53. $this->password = $_POST['password'];
  54. if($this->user === 'demo' && $this->password === 'demo'){
  55. return header('Location: dashboard');
  56. }
  57. }
  58.  
  59. public function dashboard(){
  60. $data = array('username'=> $this->user,'message'=> 'Login success');
  61. return $this->tpl->renderTemplate('dashboard', $data);
  62. }
  63.  
  64. }
  65.  
  66. ?>
  67.  
  68. <?php
  69.  
  70. class Model{
  71.  
  72. public function __construct(){
  73.  
  74. }
  75.  
  76.  
  77. }
  78.  
  79. ?>
  80.  
  81. <?php
  82.  
  83. class View{
  84.  
  85. public function __construct(TemplateLoader $tpl){
  86. $this->tpl = $tpl;
  87. }
  88.  
  89. }
  90.  
  91. ?>
  92.  
  93. <?php
  94.  
  95. class Controller{
  96.  
  97. public function __construct($Model,TemplateLoader $TemplateLoader){
  98. $this->model = $Model;
  99. $this->tpl = $TemplateLoader;
  100. }
  101.  
  102. public function index(){
  103. return 'Index';
  104. }
  105.  
  106. }
  107.  
  108. ?>
  109.  
  110. <html>
  111. <body>
  112.  
  113. <h1>Test login MVC</h1>
  114.  
  115. <form method="POST" action="doLogin">
  116. <input type="text" name="username" />
  117. <br>
  118. <input type="password" name="password" />
  119. <br>
  120. <input type="submit" value="LOGIN"/>
  121. </form>
  122.  
  123. </body>
  124. </html>
  125.  
  126. <html>
  127. <body>
  128. <h1>Test MVC Dashboard</h1>
  129. <p>Ciao <?php echo $username; ?></p>
  130. <p><?php echo $message; ?></p>
  131. </body>
  132. </html>
Add Comment
Please, Sign In to add comment