PHP Suit - Framework https://www.php-suit.com/taxonomy/term/2/0 en MVC framework skeleton - I https://www.php-suit.com/mvc-framework-skeleton-i <p>How to write your very own PHP MVC framework - part 1: Front controller and .htaccess rules.<br /> &lt;!--break--><br /> My goal is to show how to create simple MVC framework step by step.</p> <p>Assume simple framework with the file layout simillar to my framework, as described <a href="/application-layout">here</a>:</p> <p><code>/class/ (...) site-specific classes<br /> /controllers/ (...) site-specific controllers<br /> /pages/ (...) site-specific HTML pages / templates<br /> /images/ (...) images (accessed directly due to .htaccess rules)<br /> /layout/ (...) other site-specific layout files (css, js...)<br /> /.htaccess - basic rules for HTTP requests redirecting<br /> /index.php - the site front controller</code></p> <h3>.htaccess file</h3> <p>The most important thing is HTTP request redirecting - you've to redirect all requests to front controller <em>index.php</em> instead of directories and files. This work can be done by <strong>.htaccess</strong> file (on systems based on Apache HTTP server, which is the most used server, with the Mod_Rewrite enabled - consult your hosting's FAQs if you aren't sure).</p> <p><a href="/htaccess-file">The .htaccess file</a> should be in the web root directory and it should contains something like:</p> <p><code>Options +FollowSymLinks +ExecCGI<br /> &lt;IfModule mod_rewrite.c&gt;<br /> RewriteEngine On<br /> RewriteCond %{REQUEST_URI} !/images<br /> RewriteRule ^(.*\.(jpg|gif|png))$ /images/$1 [L,PT]<br /> RewriteCond %{REQUEST_URI} !/layout<br /> RewriteRule ^(.*\.(css))$ /layout/$1 [L,PT]<br /> RewriteCond %{REQUEST_URI} \..+$<br /> RewriteCond %{REQUEST_URI} !\.html$<br /> RewriteCond %{REQUEST_FILENAME} -f<br /> RewriteRule .* - [L]<br /> RewriteRule ^(.*)$ index.php [QSA,L]<br /> &lt;/IfModule&gt;</code></p> <p>These rules says that the files *.jpg, *.gif and *.png resides in the /images directory, *.css are in the /layout directory and all other files are handled by the <em>index.php</em> script.</p> <h3>Index.php - front controller</h3> <p>The first thing you have to do in the index.php controller is to parse requested URL and decide what controller/view should be used. The URL routing will be discussed in the next part. But routing isn't the only thing we can do in the front controller.</p> <h4>The things the front controller should do</h4> <p>Here is the list of the things the front controller should do:</p> <ol> <li>Define some common functions and constants</li> <li>Parse required URL</li> <li>Make redirtects etd.</li> <li>Call the controller</li> <li>Provide the mechanism for views</li> <li>Deal with all runtime errors</li> </ol> <h4>Autoloader</h4> <p>In my index.php the <em>__autoload()</em> magic function is declared - the simpliest autoloader should look like this:</p> <p><pre name="code" class="php">function __autoload($class_name) { $class=strtolower($class_name); $fn = './class/'.$class . '.php'; if (file_exists($fn)) { require_once($fn); return; } die('Class '.$class_name.' not found'); }</pre></p> <p>This autoloader assumes that every required class is in the file with the same name - e.g. class "MediaDirectory" is defined in the "mediadirectory.php" file, which is stored in the /class directory.</p> <h4>Localhost flag</h4> <p>A good idea is to decide if the server is localhost (development) or production. You should make the decision this way:</p> <p><pre name="code" class="php">$localhost = ($_SERVER['SERVER_ADDR'] == '127.0.0.1')? true: false; define ('LOCALHOST',$localhost, 1);</pre></p> <p><em>To be continued...</em></p> https://www.php-suit.com/mvc-framework-skeleton-i#comments Framework How To... Sun, 14 Sep 2008 17:39:00 +0000 admin 17 at https://www.php-suit.com