Advertisement
sunco

Some MINI3 adjustments

Mar 30th, 2017
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. First, MINI3 is great, very easy to understand, great work
  6.  
  7. I made this View class to fit what I want to accomplish
  8.  
  9. Why? Let me explain
  10.  
  11. What about if you rename header.php file? Then you need to change a lot of code parts
  12. What about an ajax call? Doesn't need header and footer, so..
  13. What about if you want to call a controler/method from a View file?
  14.  
  15. */
  16.  
  17. // file - application/Core/View.php
  18.  
  19. namespace Mini\Core;
  20.  
  21. class View
  22. {
  23.     public function render($filename, $data = null, $template = true)
  24.     {
  25.         if ($data) {
  26.             foreach ($data as $key => $value) {
  27.                 $this->{$key} = $value;
  28.             }
  29.         }
  30.        
  31.         if ($template == true) {
  32.             require APP . 'View/_templates/header.php';
  33.         }
  34.        
  35.         require APP . 'View/'.$filename.'.php';
  36.        
  37.         if ($template == true) {
  38.             require APP . 'View/_templates/footer.php';
  39.         }
  40.     }
  41.    
  42.     public function requestAction($datos) {
  43.         $url = "http:".URL.$datos; // doesn't work without http:
  44.        
  45.         $cookies = "";
  46.        
  47.         foreach($_COOKIE as $a => $b) {
  48.             $cookies .= $a."=".$b."; ";
  49.         }
  50.        
  51.         $ch = curl_init();
  52.         curl_setopt($ch, CURLOPT_URL, $url);
  53.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  55.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  56.         curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  57.         curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
  58.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  59.         curl_setopt($ch, CURLOPT_COOKIE, $cookies);
  60.         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
  61.         $result = curl_exec($ch);
  62.         curl_close($ch);
  63.        
  64.         return $result;
  65.     }
  66. }
  67.  
  68. // render usage ******************************************************************************
  69.  
  70. namespace Mini\Controller;
  71.  
  72. use Mini\Core\View;
  73.  
  74. class StatsController
  75. {
  76.     var $View;
  77.    
  78.     function __construct() {
  79.         $this->View = new View();
  80.     }
  81.    
  82.     function index()
  83.     {
  84.         $data["title"] = "Stats"; /* for <title></title> inside header.php in this case */
  85.        
  86.         $this->View->render('stats/index', $data);
  87.     }
  88.    
  89.     function some_ajax_call() {
  90.         // call methods, etc..
  91.         // $data
  92.        
  93.         $this->View->render('stats/some_stuff', $data, false); // false - prevent to render header and footer
  94.     }
  95. }
  96.  
  97. // requestAction usage ******************************************************************************
  98.  
  99. // in a View file.. let's say in application/View/stats/some_file.php
  100. echo $this->requestAction("controller/method");
  101.  
  102. /*
  103.  
  104. ToDo:
  105.  
  106. - integrate into a native/polished way
  107. - params in requestAction (easy but no time this right moment)
  108.  
  109. */
  110.  
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement