Solved

Router for PHP reqeusts

Posted on 2016-10-03
12
29 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
12 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 400 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
If there is any good router already present
Laravel
0
 
LVL 1

Author Comment

by:Vipin Kumar
Comment Utility
@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 51

Expert Comment

by:Julian Hansen
Comment Utility
.htaccess
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:Vipin Kumar
Comment Utility
@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 51

Expert Comment

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

Author Comment

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

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
@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 51

Expert Comment

by:Julian Hansen
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now