0

I have written a basic Router class for my MVC, however I'm having trouble to use hyphen in my links, it gives a 403 Forbidden for existing links, however for non-existing ones it prints them out correctly and gives a 404 error page.

My controller classes are of the form Link_Here and I change the hyphen to underscore in the router. The URL structure is http://example.com/{$controller}/($action)/($parameters) and the issue is the controller part

Here's my router code:

    <?php

class Router
{
    private $url, $controller, $method, $params;
    private $allowedChars = array('-', '_', '/', '\\', '.');

    public function __construct()
    {
        if(!empty($_GET['page']))
        {
            if(ctype_alnum(str_replace($this->allowedChars, '', $_GET['page'])))
            {
                $this->url = $_GET['page'];
            }
            else
            {
                throw new Exception("Malformed URL");
            }
        }
        else
        {
            $this->url = 'index';
        }

        $this->url = explode('/', $this->url);

        // This is where I change the hyphen to an underscore
        $this->controller = implode('_', array_map('ucfirst', explode('_', str_replace('-', '_', array_shift($this->url)))));
        $this->method = array_shift($this->url);
        $this->params = &$this->url;
    }

    public function commit()
    {
        if(class_exists($this->controller))
        {
            if(method_exists($this->controller, $this->method) && empty($this->params))
            {
                if(empty($this->params))
                {
                    $ctrl = new $this->controller;
                    $ctrl->loadModel($this->controller);
                    $ctrl->{$this->method};
                }
                else
                {
                    $ctrl = new $this->controller;
                    $ctrl->loadModel($this->controller);
                    $ctrl->{$this->method}($this->params);
                }
            }
            else
            {
                $ctrl = new $this->controller;
                $ctrl->loadModel($this->controller . '_Model');
                $ctrl->index();
            }
        }
        else
        {
            $ctrl = new Error;
            $ctrl->loadModel('Error');
            $ctrl->notFound();
        }
    }
}

And my rewrite rules:

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    Options -Indexes

    RewriteEngine On
    # RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
  • How are your URLs structured? domain.com/{controller}/{action}/{parameters}? Where do hyphens come in, in parameters? – Martin Bean Nov 06 '13 at 17:20
  • @Martin Bean: Yes, that's how it's structured. The hyphens come in the controller part, but I change them to underscores with str_replace because I can't write classes names with hyphens –  Nov 06 '13 at 17:37
  • [This](http://stackoverflow.com/a/19309893/727208) should help, I think. – tereško Nov 06 '13 at 17:44
  • To be honest, I don’t know why you’re writing a new router from scratch. There must be hundreds of routing components written in PHP already. Symfony has a good one, and there’ll be dozens more on Packagist. – Martin Bean Nov 06 '13 at 18:11
  • @tereško Don’t insult my code. We’re all learning, including you, O Might One. – Martin Bean Nov 06 '13 at 18:56
  • @MartinBean I don't need a Router, I want to know how it works and how to code one. If people followed your suggestion there wouldn't be dozens of them on Packagist, just one. –  Nov 06 '13 at 19:16

0 Answers0