is there any good way of hiding the complexity of

2019-09-06 19:18发布

问题:

i am building a web site using PHP and TWIG, i have organized my code into: class folder: for php classes lib folder: for non-classes php files templates folder: for twig templates and index.php file

when i want to include a link for register page for example the link will be: domain-name/lib/register.php the question is: is there any good way of hiding the file organization from the link for example to make the link something like: domain-name/register without changing my file organization and preserving the ability to send get parameters in the url?

thanks

回答1:

Here's an example htaccess file I would use. Folder structures entered after "register/" are treated as variables that forward to register.php. The QSA flag will allow you to have additional GET variables treated as vars if need be. (e.g. /register/something/?some_var=1).

This is for specific cases when you know how many variables you want to rewrite for what pages. In other words, the below will only work with two variables/spots (e.g. /registers/var1/var2/).

Hope that helps!

############################################
## ENABLE REWRITES
RewriteEngine on
Options +FollowSymLinks

## EXAMPLE WITH 1 VAR
RewriteRule ^register/([A-Za-z0-9-]+)/ lib/register.php?var1=$1 [L,QSA]

## EXAMPLE WITH 2 VAR
RewriteRule ^register/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ lib/register.php?var1=$1&var2=$2 [L,QSA]

## ALSO GOOD TO HAVE ERROR DOCS REWRITE
ErrorDocument 404 oops/


回答2:

If you are using apache, you can use the mod_rewrite module with a .htaccess file http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

For other webservers similar modules and methods exist.



标签: php twig