Solved

Router for PHP reqeusts

Posted on 2016-10-03
12
59 Views
Last Modified: 2016-11-16
Hi,

I want to create a router for my web application, so that all the requests are redirected to index.php and then index.php will route  the requests to the respective scripts. This is just to achieve clean URLs for my web application, so that a URL http://localhost/portal/admin.php?var1=val1&var2=val2 is seen in browser address bar as http://localhost/portal/index.php/admin/var1/val1/var2/val2

If there is any good router already present, kindly let me know the link to it as well as an example of how to use it so that I can implement in my web application accordingly.

Kindly let me know if any more information is required.

Thanks in advance.
0
Comment
Question by:Vipin Kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41827524
The first thing is to quit using 'localhost' and use at the IP address of the machine.  If you don't you may find that your application ONLY works on 'localhost' and no one else will be able to use it.  The only value of 'localhost' is to run things that you Don't want others to access.
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 400 total points
ID: 41827553
You need to look at URL re-writing - with Apache this uses the mod_rewrite module in conjunction with an .htaccess file. In the latter you specify rules that redirects everything to index.php.

Index.php then looks at the url parameters and uses an internal router to decide where to send those requests

This article may be of some use
https://www.experts-exchange.com/articles/13095/A-simple-PHP-master-Template-framework.html
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 41828148
If there is any good router already present
Laravel
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828189
@Ray,

I tried laravel... but i have to learn it completely and I have no idea of MVC... so I tried clean URLs using the .htaccess but it was getting very complex with Regex... so thought that if I can achieve that through Router... does building a Router also has to go through the concept of MVC.. is there any way I can achieve clean URLs..
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41828225
.htaccess
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828228
@Julian,

I tried that too.. with the below question i got a solution also, but the problem is that if you check the header then there are multiple redirection happening which is unwanted

https://www.experts-exchange.com/questions/28971545/Generic-Rule-to-Redirect-and-Rewrite-URL-using-htaccess.html
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41828243
if you check the header then there are multiple redirection happening which is unwanted
Explain
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828262
@Julian.. shall I open a new question, so that you can help me achieve the clean URL stuff??
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41828315
I thought that was what this thread was for. Did you read the article I posted above?
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41831909
@Julian,

I have gone through the article that you posted, I just asked for opening a new question is because what I mentioned about .htaccess it was a bit off topic for this question as it was related to router for PHP

Shall I open a new question or you would like to answer the .htaccess stuff in this question only.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41832009
I think .htaccess is core to the question - it is necessary for any kind of routing solution through a master script (index.php).
Why not take a leaf out of WordPress book and look at their .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sitebase/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sitebase/index.php [L]
</IfModule>

# END WordPress

Open in new window

Which says unless the specific file or path exists re-route everything to index.php.
Once you have all friendly URL's flowing to index.php you can build your routing rules internally to decide how to deal with the URL
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question