How to Implement URL Routing in PHP
If you use Apache you can do the URL routing via mod_rewrite.
Small example:
RewriteEngine On
RewriteRule ^(dir1)/?(path2)? main.php?dir=$1&path=$2
That'll have any request like
http://yoursite.com/dir1/path1
served by
http://yoursite.com/main.php?dir=dir1&path=path2
More examples here.
The other alternative have every request redirect to a single php file
RewriteEngine On
RewriteRule (.*) main.php?request=$1
and then to do it in code, where you can use a similar approach, by having a set of regular expressions that are matched by some code and then redirected via header() or just internally.
PHP Application URL Routing
I prefer to use reg ex over making my own format since it is common knowledge. I wrote a small class that I use which allows me to nest these reg ex routing tables. I use to use something similar that was implemented by inheritance but it didn't need inheritance so I rewrote it.
I do a reg ex on a key and map to my own control string. Take the below example. I visit /api/related/joe
and my router class creates a new object ApiController
and calls it's method relatedDocuments(array('tags' => 'joe'));
// the 12 strips the subdirectory my app is running in
$index = urldecode(substr($_SERVER["REQUEST_URI"], 12));
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)/post$#Di" => "ThreadController/post/title",
"#^thread/(.*)/reply$#Di" => "ThreadController/reply/title",
"#^thread/(.*)$#Di" => "ThreadController/thread/title",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
In order to keep errors down and simplicity up you can subdivide your table. This way you can put the routing table into the class that it controls. Taking the above example you can combine the three thread calls into a single one.
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)$#Di" => "ThreadController/route/uri",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
Then you define ThreadController::route to be like this.
function route($args) {
Route::process($args['uri'], array(
"#^(.*)/post$#Di" => "ThreadController/post/title",
"#^(.*)/reply$#Di" => "ThreadController/reply/title",
"#^(.*)$#Di" => "ThreadController/thread/title",
));
}
Also you can define whatever defaults you want for your routing string on the right. Just don't forget to document them or you will confuse people. I'm currently calling index if you don't include a function name on the right. Here is my current code. You may want to change it to handle errors how you like and or default actions.
How can I create route URL for views in core PHP?
If I get it right, you want to use clean (SEO) links instead of calling directly php scripts. Solution depends on web server you are using.
For apache (one of the most used ones) you must make redirection from your "imaginary" paths (i.e. /app/login) to your real script, which will handle the request.
Again, most used solution for apache is to add your redirection rules in .htaccess file. From there apache server will read your rule for every request to your server and if conditions are met redirection will be made.
There are many tutorials on how to do that, i.e.
https://www.youtube.com/watch?v=1pbAV6AU99I
Or: https://moz.com/blog/using-mod-rewrite-to-convert-dynamic-urls-to-seo-friendly-urls
URL Routing in PHP : url routes ony to 404-Not-Found page
The problem was with .htaccess file. I didn't include an .htaccess file which will re-route according to the routings I had in index.php file(the one in the root directory).
I'm posting the .htaccess code below
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /das/index.php [L]
How to keep a website with url routing directory independent
You could put in the head
<base href="<?php echo url::base(); ?>" />
This will mean the browser will request any non-absolute URLs relative to that path. However I am not sure how this would affect URLs embedded in CSS files etc. This does not affect paths defined in CSS files. (thanks mooware)
Routing URLs in PHP
Use mod_rewrite
to route everything to a single index.php
file. Then check the variable in $_SERVER['REQUEST_URI']
within this file to dispatch to the required handler.
This configuration will enable mod_rewrite
, if it's installed:
DirectorySlash Off
Options FollowSymLinks Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]
Related Topics
PHP Variable Inside Echo 'HTML Code'
PHP Prepend Leading Zero Before Single Digit Number, On-The-Fly
Asynchronous Function Call in PHP
Understanding MVC: Whats the Concept of "Fat" on Models, "Skinny" on Controllers
How to Get Time Zone Through Ip Address in PHP
Publishing to User's Wall Without Being Online/Logged-In - Facebook Sharing Using Graph API
Differencebetween {{ }} and {!! !!} in Laravel Blade Files
Update Query with Pdo and MySQL
PHP What Is the Best Approach to Using Xml? Need to Create and Parse Xml Responses
Generating a Single Entity from Existing Database Using Symfony2 and Doctrine
Get Current Url Path with Query String in PHP
Error 403 in Loading Resources Like CSS and Js in My Index.Php