Fall back to PHP interpreter if a file is non existant [duplicate] - php

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:
"http://stackoverflow.com/questions/ask"
Where as with my knowledge of PHP programming I would probably code it like so:
"http://stackoverflow.com/index.php?p=questions&act=ask"
Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.

The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:
RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]
However, the recommended way in Apache 2.5 is with FallbackResource:
FallbackResource index.php
IIS has similar functionality if that's what you're using.
In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:
require '../router.php';
$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);
Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.

They are most likely using the same method you are describing (embedding the navigation variables) within the URL, but it is being done "under the hood".
The mechanism that allows you to present URLs such as this is called MOD Rewrite. It uses the combination of the variables in the URL, and regular expressions to re-represent the URL to the end-user in a more user-friendly manner.
More Information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Edit: Of course this would apply to code running on apache webserver. There are probably similar modules for other web servers such as IIS.
Also, mind you that mod_rewrite is outside of the scope of php. It is instead actually apache server directives, which are invoked before php even comes into play.

I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look:
https://github.com/c9s/Pux
Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.

Introducing RouteREST
https://github.com/iPhoneSDKPro/routeREST
This is my implementation of a php router. It uses MVC architecture and is designed to be simple to setup and use.
I use this in a project I developed and thought to share with the community.
At some point I would like to make it into a "Composer" library.. someday..
Enjoy
-James

Related

Why route Restful requests through a .htaccess file vs a router (front controller)?

Backstory:
I am attempting build an API with REST architecture where performance is numero uno.
Team has experience with a solution (from a previous project) that routes all calls through an .htaccess file that then forwards to correct controller (.htaccess->controller(resource)->response). This is pretty fast, but the code isn't very flexible nor does it adhere to SOLID principles (overall I find it difficult to work with, plain and simple).
The current model is based off of http://coreymaynard.com/
Example:
.htaccess file:
RewriteRule api/logins/(.*)$ api/controllers/Logins.php?request=$1 [QSA,NC,L]
Controller:
logins->get_login; logins->post_login; etc etc. Extends a base class that reads the request and builds what method to execute.
I have been doing some serious research into API solutions and frameworks; Slim, Phalcon, Laravel, etc etc, with some basic (and mostly biased) performance testing for comparison with the adhoc system. The adhoc outperformed Slim and was neck to neck with Phalcon - we are not going with phalcon because it's not that well trusted within the team. So our solution is to write a minimal php application. I do not want to use the existing API system, and do something akin to a front controller.
I really like how Slim constructs resource routes, like so:
$app = new Slim/Slim();
//Add Get Route
$app->get('/my/route', Function/Map to Object/Do something else);
To me, this is readable, easy to maintain, and even easier to scale. Then I discovered this little gem on the web:
Small and Fast Controller with PHP
Based on these examples, I prototyped out a route controller and shared it with my team.
Questions:
1) What kind of performance hit am I going to see as I add additional routes? I haven't been satisfied with the information out there with executing routes this way (very procedural, stops handled by exceptions), nor do I have the experience to make assumptions.
2a) Am I being too cynical of the .htaccess method of routing? Is the server overhead generated for parsing an .htaccess file any real concern?
2b) Has anyone had experience building php applications or REST applications this way?
And as always, thank you for taking the time to read and respond.
UPDATE: We are moving to an Nginx server, meaning limited access to defining routes through Nginx's config and decided on a front controller to define our routes.
"UPDATE: We are moving to an Nginx server, meaning limited access to defining routes through Nginx's config and decided on a front controller to define our routes."
This screams to me that defining routes through .htaccess is ill advised. Your front controller should theoretically work on nginx or apache without modification, and your application would continue to operate as expected. I believe you've made the right decision.

Replicate Google App Engine app.yaml handlers in PHP outside of GAE

I am considering building my PHP project in Google App Engine (GAE), and I would like to be confident that I could easily port my code to a more standard Apache Tomcat & PHP server if needed. The one aspect that I cannot find a standard implementation for is GAE's app.yaml handlers. Let me define the functionality that I care about:
The ability to explicitly route incoming requests to a specific php script for fulfillment based on URL patterns.
My searches thus far have lead me to the Tomcat .htaccess RewriteEngine. However, it seems like this literally re-writes the URL and redirects the client machine to that URL. Am I wrong? Other than this solution, I have found nothing else that is promising. Can someone suggest a replacement for GAE's app.yaml handlers?
Thanks in advance!
Seems like you are looking for Apache mod_rewrite which lets you setup rewrite rules that are interpreted behinds the scenes and do not change the external URL.

Routing page requests with PHP the right way

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:
"http://stackoverflow.com/questions/ask"
Where as with my knowledge of PHP programming I would probably code it like so:
"http://stackoverflow.com/index.php?p=questions&act=ask"
Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.
The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:
RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]
However, the recommended way in Apache 2.5 is with FallbackResource:
FallbackResource index.php
IIS has similar functionality if that's what you're using.
In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:
require '../router.php';
$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);
Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.
They are most likely using the same method you are describing (embedding the navigation variables) within the URL, but it is being done "under the hood".
The mechanism that allows you to present URLs such as this is called MOD Rewrite. It uses the combination of the variables in the URL, and regular expressions to re-represent the URL to the end-user in a more user-friendly manner.
More Information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Edit: Of course this would apply to code running on apache webserver. There are probably similar modules for other web servers such as IIS.
Also, mind you that mod_rewrite is outside of the scope of php. It is instead actually apache server directives, which are invoked before php even comes into play.
I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look:
https://github.com/c9s/Pux
Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.
Introducing RouteREST
https://github.com/iPhoneSDKPro/routeREST
This is my implementation of a php router. It uses MVC architecture and is designed to be simple to setup and use.
I use this in a project I developed and thought to share with the community.
At some point I would like to make it into a "Composer" library.. someday..
Enjoy
-James

Need php template help and clean url help

I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.
If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.
If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.
As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.
Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess
You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).
In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).
I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.
your answer is the htaccess file, is converts the 'folder structure' to $_GET value's
for example,
if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile
htaccess:
RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA]
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA]

mod_rewrite before of after developing application

I am trying to better understand how to incorporate mod_rewrite into a web applications development life cycle.
The question I am trying to understand is:
Do you build your application and then setup mod_rewrite after the fact?
Is it more appropriate to consider mod_rewrite along the way while you develop your application?
mod_rewrite is a substantial part of your application. If it doesn't handle just trivial requests, but for example establishes a front controller, then of course you have to have planned your application structure to know what mod_rewrite is supposed to do.
Whenever I'm building an application - first thing what I do is to redirect any /foo/bar/ or /foo/bar.html into a PHP index.php?path=foo/bar. More detailed setup ( for example for images etc. ) I do when I need it

Categories