Search code examples
phpcssurl-routing

my css file not loading when using "pretty-url" routing


So in my application, suppose http://localhost:8888/mvc/user/login this is a URL. so in this url user is my controller and login is the method in user controller and this is coded in my core files like this:

    <?php
/**
 * 
 */
class Bootstrap 
{

    function __construct()
    {
        $url = isset($_GET['url'])?$_GET['url']:null;

        $url = rtrim($url,'/');
        $url = explode('/', $url);
        //print_r($url);



        if (empty($url[0])) {
            require 'controllers/welcome.php';
            $controller = new Welcome();
            $controller->index();
            return false;
        }

        $file = 'controllers/'.$url[0].'.php';
        if (file_exists($file)) {
            require $file;
        } else {
            $this->showError();
        }

        $controller = new $url[0];
        //calling methods 
        if (isset($url[2])) {
            if (method_exists($controller, $url[2])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->showError();
            }

        } else {
            if(isset($url[1])){ 
                if (method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->showError();
                }

            } else {
                $controller->index();
            }
        }


    }

    public function showError() {
        require 'controllers/CustomError.php';
        $controller = new CustomError();
        $controller->index();
        return false;
    }

}

Now my issue is when i try to load css files in views my URL becomes like this:

 http://localhost:8888/mvc/public/css/style.css

So now according to above code, it is assuming that public is a controller and CSS is a method. and it is giving an error. So how can I solve this problem?

I hope you guys understood what is the problem. Thanks.


Solution

  • try the following for your CSS file in head section of your view:

    <link rel="stylesheet" type="text/css" href="http://localhost:8888/mvc/public/css/style.css">