PHP-friendly URL with htaccess

Asked

Viewed 51 times

0

Good morning,

I am creating a simple system in PHP to learn more about MVC. Would you like to retrieve from the URL the information contained in '?pag='. How to do this? I need to change my file .htaccess?

I want the URL to look like controller/action/param/? pag=0

Bootstrap.php

<?php

class Bootstrap
{
    private $controller = 'indexController';
    private $action     = 'index';

    public function __construct()
    {
        $url = $this->parseUrl();

        if (isset($url[0])) {
            if (file_exists(CONTROLLERS . $url[0] . 'Controller.php')) {
                $this->controller = $url[0] . 'Controller';
            }
        }

        if (isset($url[1])) {
            $this->action = $url[1];
        }

        require_once CONTROLLERS . $this->controller . '.php';
        $this->controller = new $this->controller;
        $this->controller->{$this->action}();
    }

    private function parseUrl()
    {
        if (isset($_GET['url'])) {
            $url = rtrim($_GET['url'], '/');
            $url = explode('/', $url);

            return $url;
        }
    }
}

.htacess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?url=$1 [L]

  • Not exactly. There is a difference: in this case there is a routing for controller/action/param to then see another variable.

  • Friend, just apply the same concept of the parameter page, but updated that answer added the concept "MVC".

No answers

Browser other questions tagged

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