Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- First, MINI3 is great, very easy to understand, great work
- I made this View class to fit what I want to accomplish
- Why? Let me explain
- What about if you rename header.php file? Then you need to change a lot of code parts
- What about an ajax call? Doesn't need header and footer, so..
- What about if you want to call a controler/method from a View file?
- */
- // file - application/Core/View.php
- namespace Mini\Core;
- class View
- {
- public function render($filename, $data = null, $template = true)
- {
- if ($data) {
- foreach ($data as $key => $value) {
- $this->{$key} = $value;
- }
- }
- if ($template == true) {
- require APP . 'View/_templates/header.php';
- }
- require APP . 'View/'.$filename.'.php';
- if ($template == true) {
- require APP . 'View/_templates/footer.php';
- }
- }
- public function requestAction($datos) {
- $url = "http:".URL.$datos; // doesn't work without http:
- $cookies = "";
- foreach($_COOKIE as $a => $b) {
- $cookies .= $a."=".$b."; ";
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
- curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_COOKIE, $cookies);
- 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");
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- }
- // render usage ******************************************************************************
- namespace Mini\Controller;
- use Mini\Core\View;
- class StatsController
- {
- var $View;
- function __construct() {
- $this->View = new View();
- }
- function index()
- {
- $data["title"] = "Stats"; /* for <title></title> inside header.php in this case */
- $this->View->render('stats/index', $data);
- }
- function some_ajax_call() {
- // call methods, etc..
- // $data
- $this->View->render('stats/some_stuff', $data, false); // false - prevent to render header and footer
- }
- }
- // requestAction usage ******************************************************************************
- // in a View file.. let's say in application/View/stats/some_file.php
- echo $this->requestAction("controller/method");
- /*
- ToDo:
- - integrate into a native/polished way
- - params in requestAction (easy but no time this right moment)
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement