phpmodel-view-controllerroutescustom-routes

CSS problems with custom Router - PHP


I am creating a custom Router for my web app. I use MVC.

When I, for example, type fab.app/portfolio all is good. But when I type fab.app/portfolio/ the css, images, and js are not displayed.

This is because the paths change. In the first case the path that it is looking for the images is fab.app/images/(the_image) but in the second case it is fab.app/portfolio/images/(the_image).

Also, in the index.php I have to have an entry for both the url with and without the slash in the end. Which I don't like. I would prefer to write it without the slash and it should work with the slash as well.

Below you can find the router and index.php


index.php

<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../app/setup.php';

use Fab\Controllers;
use Fab\Router;

$router = new Router\Router();

$router->get('/', 'MainController', 'index');
$router->get('/portfolio', 'ItemsController', 'showAllItems');
$router->get('/portfolio/', 'ItemsController', 'showAllItems');
$router->get('/portfolio/[\w\d]+', 'ItemsController', 'single_item');
$router->get('/about', 'MainController', 'about');
$router->get('/contact', 'MainController', 'contact');
$router->get('/admin/dashboard', 'AdminController', 'index');
$router->get('/admin/dashboard/addItem', 'AdminController', 'addItem');
$router->get('/admin/dashboard/deleteItem', 'AdminController', 'deleteItem');
$router->get('/admin/dashboard/editItem', 'AdminController', 'editItem');
$router->get('/admin/dashboard/contactSupport', 'AdminController', 'contactSupport');

$router->post('/admin/addItem', 'AdminController', 'postAddItem');
$router->post('/admin/deleteItem', 'AdminController', 'postDeleteItem');
$router->post('/admin/editItem', 'AdminController', 'postEditItem');

//$router->get('/test', 'ItemsController', 'showAllItems');

////See inside $router
//echo "<pre>";
//print_r($router);

$router->submit();

Router.php

<?php
/**
 * Created by PhpStorm.
 * User: antony
 * Date: 5/30/16
 * Time: 3:31 PM
 */
namespace Fab\Router;

class Router
{
    private $_getUri = array();
    private $_getController = array();
    private $_getMethod = array();
    private $_postUri = array();
    private $_postController = array();
    private $_postMethod = array();

    public function __construct()
    {
    }

    /**
     * Build a collection of internal GET URLs to look for
     * @param $uri - The url that the user types in the browser
     * @param $controller - The controller that will handle the url
     * @param $method - The method of the controller that will run
     */
    public function get($uri, $controller, $method)
    {
        $this->_getUri[] = $uri;
        $this->_getController[] = $controller;
        $this->_getMethod[] = $method;
    }

    /**
     * Build a collection of internal POST URLs to look for
     * @param $uri - The url that the user types in the browser
     * @param $controller - The controller that will handle the url
     * @param $method - The method of the controller that will run
     */
    public function post($uri, $controller, $method)
    {
        $this->_postUri[] = $uri;
        $this->_postController[] = $controller;
        $this->_postMethod[] = $method;
    }

    public function submit()
    {
        $found = 0;

        if ($_SERVER['REQUEST_METHOD'] === 'GET') {

            $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

            //Map URL to page
            foreach ($this->_getUri as $key => $value)
            {
                if ( $found = preg_match("#^$value$#", $path) )
                {
//                    echo $key . ' => ' . $value; //See what the $path returns

                    //Find parameter if passed
                    $parts = explode('/', $path);
                    count($parts) > 2 ? $parameter=$parts[2] : $parameter=null;

                    //Instantiate Controller
                    $controller = 'Fab\Controllers\\' . $this->_getController[$key];
                    $controller = new $controller($parameter);

                    //Call the appropriate method
                    $method = $this->_getMethod[$key];
                    $controller->$method();

                    break;
                }
            }

            //Show 404 page
            if ( $found == 0 )
            {
                //Instantiate Controller
                $controller = 'Fab\Controllers\MainController';
                $controller = new $controller();

                //Call the appropriate method
                $method = 'error404';
                $controller->$method();

                die();
            }

        } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {

            $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

            foreach ($this->_postUri as $key => $value)
            {
                if ( $found = preg_match("#^$value$#", $path))
                {
//                    echo $key . ' => ' . $value; //See what the $path returns

                    //Find parameter if passed
                    $parts = explode('/', $path);
                    count($parts) > 2 ? $parameter=$parts[2] : $parameter=null;

                    //Instantiate Controller
                    $controller = 'Fab\Controllers\\' . $this->_postController[$key];
                    $controller = new $controller($parameter);

                    //Call the appropriate method
                    $method = $this->_postMethod[$key];
                    $controller->$method();

                    break;
                }
            }

            //Show 404 page
            if ( $found == 0 )
            {
                //Instantiate Controller
                $controller = 'Fab\Controllers\MainController';
                $controller = new $controller();

                //Call the appropriate method
                $method = 'error404';
                $controller->$method();

                die();
            }
        }

    }

}

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    AddType 'text/css; charset=UTF-8' css

</IfModule>

Example of calling css (but also images and js) in html

<link href="/css/my-admin-custom.css" rel="stylesheet">

-------------------------------------------------------------------

For sake of clarity, here is how I resolved the issue (per @Max13's answer). This goes into Router.php:

        /**
         * If last char in URL is '/' redirect without it 
         * and also check if url is root '/' because this would result 
         * in infinite loop
         */
        if ( ($path[strlen($path)-1] === '/') && !($path === '/') ) { //
            $newPath = substr($path, 0, -1);
            header("Location: $newPath", true, 302);
            exit;
        }

Solution

  • With and without trailing slash doesn't mean the same directory level, so I don't think it would be a good idea to "accept" both, in your router.

    So, a good solution IMHO would be: During your checks, if the path ends with "/", then send a 302 Found header without the trailing slash.

    -- Edit --

    See (For header redirects and meta tag redirection, both are often used at the same time): https://www.arclab.com/en/websitelinkanalyzer/http-and-meta-redirects.html