Mini Framework Mvc

Asked

Viewed 91 times

0

This is the mistake you are making when I call this method in my controller

Warning: end() expects Parameter 1 to be array, null Given in C: xampp htdocs php_mvc Libs System.php on line 55

Warning: array_pop() expects Parameter 1 to be array, null Given in C: xampp htdocs php_mvc Libs System.php on line 56

My class that configures Urls

class System
{
    private $Url;

    private $Explode;

    public $Controller;

    public $Action;

    public $Params;

    function __construct()
    {
        $this->setUrl();
        $this->setExplode();
        $this->setController();
        $this->setAction();
        $this->setParams();
    }


    //Recebe as Urls
    private function setUrl(){
        $_GET['url'] = $_GET['url'] ? $_GET['url']."/" : "Index/Index";
        $this->Url = $_GET['url'];
    }

    //Transforma a url em Arrays
    private function setExplode(){
        $this->Explode = explode("/", $this->Url);
    }

    //Seleciona o Controller
    private function setController(){
        $this->Controller = $this->Explode[0];
    }


    //Seleciona a Action
    private function setAction(){
        $action = ($this->Explode[1] || $this->Explode[1] == null || $this->Explode[1] == "Index"  ? "Index": $this->Explode[1]);
        $this->Action = $action;
    }

    public function setParams() {
        unset($this->Explode[0]);
        unset($this->_explode[1]);

         if (end($this->Explode) == null )
             array_pop($this->Explode);

         $i = 0;
         if (!empty($this->Explode)) {
             foreach ($this->Explode as $val) {
                if($i % 2 == 0) {  // %= indice de 2
                    $ind[] = $val;
                } else {
                    $value[] = $val;
                }
                $i++;
             }
         } else {
             $ind = array();
             $value = array();
         }

         if(count($ind) == count($value) && !empty($ind) && !empty($value))
             $this->Params = array_combine ($ind, $value);
         else
             $this->Params = array();
         }




    public function getParams($name = null) {
        if($name != null){
            return $this->Params[$name];
        }else{
            return $this->Params;
        }
    }

    //Inicia as Urls
    public function run(){
        $controller_path = CONTROLLERS.$this->Controller.".php";

        if (!file_exists($controller_path)) {
            die("Houve um erro o $controller não existe");
        }else{
            require($controller_path);
            $controller = new $this->Controller();

        }

        if (!method_exists($controller, $this->Action)) {
            die("Houve um erro a {$this->Action} não existe");
        }else{
            $action = $this->Action;
            $controller->$action();
        }
    }
}

And My Controller

class Controller extends System{

    function __construct()
    {

    }

    /*Função que ira verificar o nome da View e retornará ela ao
    seu Controller*/
    protected function View($View)  {

        return require('View/'.$View.'.php');
        exit();
    }
}

My Index Controller

class Index extends Controller
{   
    function __construct()
    {

    }

    public function Index(){

        echo $this->getParams("nome");
    }
}
  • 1

    end() expects parameter 1 to be array, your $this->Explode does not appear to be an array. It is testing in home?

  • At the beginning of the function setParams() puts print_r($this->Url); and print_r($this->Explode); before the UNSET and shows us what’s coming back.

  • This returned in the $this->Url Index/Index/name/Jose/ while in the $this->Explode Array ( [0] => Index [1] => Index [2] => name [3] => Jose [4] => )

  • Where else do you use that: $this->_explode[1]? And what is your goal with this IF: if (end($this->Explode) == null )
 array_pop($this->Explode);

  • The $this->Explode[1] is the controller, $this->Explode[2] to action and it is from the IF that is to be treated the parameters that will be sent to action

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.