Escape "/" with htaccess and/or php

Asked

Viewed 67 times

1

I have a little MVC made by me. With urls of this type index.php?route=admin/produto/adicionar, but I wanted to withdraw index.php?route=, I already got some lines .htaccess stay that way www.exemplo.com/admin/produto/adicionar, the problem is that after css and js files are not found (www.exemplo.com/public/public/js/404.php):

.htaccess:

RewriteEngine On

RewriteBase /wtj/public

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

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

File structure:

inserir a descrição da imagem aqui

configs/paths.php (this is where I define the paths):

// folder name => path
return [
   "configs" => "../app/configs",
   "controllers" => "../app/controllers",
   "css" => "../public/css",
   "images" => "../public/images",
   "js" => "../public/js",
   "includes" => "../public/views/includes",
   "lang" => "../app/lang",
   "views" => "../public/views"
];

Real message on console, files to search:

GET http://localhost/wtj/public/js/404.php

GET http://localhost/wtj/public/public/css/cssAdmin/404.php

Which is the redirect programmed to my 404 page.

The right page (route) is assumed and goes to the right controller/method, but external files are troublesome.

  • Hello, thanks but I also have something like this in my project. A file that returns an array with the paths. I edited above

  • I tested it now and it actually works with absolute paths :). Thank you

1 answer

0


The problem of not finding paths is related to the relativity of paths. Instead of defining paths in /app/configs/paths.php in the form of array, define the paths at the root in /index.php through DEFINE so you can use the constant throughout the project scope by absolute path.

index php. (In any row prior to the execution of your MVC)

DEFINE('PATH_CONFIGS', 'app/configs/');
DEFINE('PATH_CONTROLLERS', 'app/controllers/');
DEFINE('PATH_LANG', 'app/lang/');
DEFINE('PATH_CSS', 'public/css/');
DEFINE('PATH_IMG', 'public/images/');
DEFINE('PATH_JS', 'public/js/');
DEFINE('PATH_VIEWS', 'public/views/');
DEFINE('PATH_INCLUDES', PATH_VIEWS.'includes/');

When it comes to calling the constant, just call it that:

echo PATH_CSS;

And if you want to continue the path path, just concatenate from the absolute path you already have:

echo PATH_CSS . "cssAdmin/seu_arquivo.css";

Browser other questions tagged

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