Facebook
From qrq, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 238
  1. <?php
  2. include_once 'config.php';
  3. include_once 'router.php';
  4. session_start();
  5. function loader( $class ) {
  6.   $directories = [
  7.     'model/',
  8.     'controller/'
  9.     ];
  10.    
  11.     foreach( $directories as $directory ) {
  12.         if( file_exists( $directory.$class.'.php') ) {
  13.           include_once $directory.$class.'.php';
  14.         }
  15.     }
  16. }
  17. spl_autoload_register('loader');
  18.  
  19. if( !isset($_GET['action']) )
  20. {
  21.   $action = $_GET['action'];
  22. }
  23. else
  24. {
  25.   $action = 'default';
  26. }
  27. $db = new DB($config);
  28. router($action,$db);
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. ^
  36. INDEX
  37. ===
  38. <?php
  39.  
  40. function router($action)
  41. {
  42.   switch($action) {
  43.     case 'default':
  44.       $controller = new DefaultController($db);
  45.       $controller->index();
  46.       break;
  47.      
  48.     case 'show':
  49.       $controller = new DefaultController($db);
  50.       $controller->show();
  51.       break;
  52.    
  53.     case 'indexlist':
  54.       $controller = new DefaultController($db);
  55.       $controller->indexlist();
  56.       break;
  57.      
  58.     default:
  59.       $controller = new DefaultController($db);
  60.       $controller->index();
  61.      
  62.   }
  63. }
  64. ========
  65. <?php
  66.  
  67. class DefaultController
  68. {
  69.     public function index()
  70.     {
  71.       $content = 'domyslna akcja';
  72.       include 'view/main.html.php';
  73.     }
  74.     public function show()
  75.     {
  76.       $content = 'inna akcja';
  77.       include 'view/main.html.php';
  78.     }
  79.  public function indexlist()
  80.     {
  81.       $content = 'akcja indexlist';
  82.       include 'view/main.html.php';
  83.     }
  84.  
  85. }
  86. =====
  87. <?php
  88.  
  89. class DB
  90. {
  91.   protected $connection;
  92.  
  93.   public function __construct($config)
  94.   {
  95.     try {
  96.       $dsn ='mysql:host='.$config['host'].';dbname='.$config['dbname'];
  97.       $this->connection = new PDO ($dsn, $config['user'],$config['password']);
  98.     }
  99.     catch (PDOException $e){
  100.       echo $e->getMessage();
  101.     }
  102.    }
  103.    public function getConnection()
  104.    {
  105.      return $this->connection;
  106.    }
  107.  }
  108. ==========
  109. <?php
  110. $config = [
  111.     'host' => 'localhost',
  112.     'dbname' => 'database',
  113.     'login' => 'root',
  114.     'password' => 'uczen4',
  115. ];
  116.  
  117. ?>
  118. =========
  119.  
  120.  
  121.  
  122.