panique / mini

Just an extremely simple naked PHP application, useful for small projects and quick prototypes. Some might call it a micro framework :)
1.35k stars 479 forks source link

[TO TEST] URL parameters are not taken in to consideration if using default controller/method #213

Open bestdamnfriend opened 8 years ago

bestdamnfriend commented 8 years ago

Analysing application.php, I see a lot of nested if statements, which causes the URL parameters to only be passed to call_user_func_array() if the selected controller and method are found, and not if the defaults are used. For example:

localhost/mini/home/index/4 sends the parameter 4 localhost/mini/home/4 throws an error localhost/mini/4 throws an error

You'd be better off checking each part of the URL individually, reverting to defaults if need be. Then, call_user_func_array will always run. This is my rewritten application.php:

class Application {

    private $url_controller = 'home';
    private $url_action = 'index';
    private $url_params = array();

    public function __construct() {

        $url = $this->parseUrl();

        if (file_exists('../application/controller/' . $url[0] . '.php')) {

            $this->url_controller = $url[0];
            unset($url[0]);

        }

        require_once '../application/controller/' . $this->url_controller . '.php';

        $this->url_controller = new $this->url_controller;

        if (isset($url[1])) {

            if (method_exists($this->url_controller, $url[1])) {

                $this->url_action = $url[1];
                unset($url[1]);

            }

        }

        $this->url_params = $url ? array_values($url) : array();

        call_user_func_array([$this->url_controller, $this->url_action], $this->url_params);

    }

    public function parseUrl() {

        if (isset($_GET['url'])) {

            return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));

        }

    }

}
panique commented 8 years ago

Thanks for the commit, I'll test this soon!

panique commented 8 years ago

btw the normal way to do this: simply dont use the default method for actions with parameters :)

bdiazc90 commented 8 years ago

Hello @panique thanks for your amazing contribution!! I have one question about the URL:

Is it a way to use the first way and invoke the home controller with the about action??

Thanks in advance!

panique commented 8 years ago

@bdiazc90 hi, thanks back! :) does a home controller and an about method exist ? if so, then your apache/nginx settings might be broken, or the installation itself has a problem. I can only recommend to use the autoinstaller and then try to reproduce the error, if you cannot reproduce then it might be your settings... Can you post more details on your problem ?

bdiazc90 commented 8 years ago

Hi @panique! Thanks for your answer, I had been looking for solving my problem in the code. I did it! I just copy all the MINI package to my localhost and update with my project. This is a great (semi) framework, I love it!

panique commented 8 years ago

Very cool! :) Glad it fixed your problem