Routing system bypassing subfolders

Asked

Viewed 133 times

0

I’m using the routing system Altorouter, and its use is very simple, use this way:

**** THIS IS THE INDEX.PHP *****

/*Incluo o arquivo do AltoRouter*/
include ('application/router.php');

$router = new AltoRouter();
$router -> setBasePath('/mvc/'); ** aqui é o diretório base **

$router -> map('GET', 'noticia', 'noticia#index', 'noticia');

$match = $router -> match();

**Aqui a estrutura da chamada controlller#action no padrão MVC**

if ($match === false) {

header('Location: /mvc/error');

} else {

list($controller, $action) = explode('#', $match['target']);

if (is_callable(array($controller, $action))) {
    $obj = new $controller;
    call_user_func_array(array($obj, $action), $match['params']);
} else {

    exit('O Controller não pôde ser chamado');

}
}

o File . htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

The problem is that when calling a view by class VIEW (where it has the call of the frontend files)... the directory of the frontend part (css, js and images) is not recognized, the system thinks it is a controller, example:

<link rel="stylesheet" href="public/main.css">

The page cannot locate the file/directory (public/main.css) and is redirected to the error page as defined in the above code.

I don’t know much about regular expressions, but I think this is it, how can I solve?

1 answer

1

In the . htacess file, add the following

RewriteCond %{REQUEST_FILENAME} !-d

This code says you should ignore the rewrite rule if you find a valid physical directory.

Another point is loading css, js and others in HTML, including for images.

To ensure path integrity, set the basis of Urls with the tag <base>

Example: <base href="http://endereço.base.do.site/" target="_blank">

http://www.w3schools.com/tags/tag_base.asp

If you don’t want to use the tag <base>, specify absolute paths or more concise relative paths.

Browser other questions tagged

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