0

I am trying to create my own PHP MVC framework for learning purpose. I have the following directory structure:

localhost/mvc:

.htaccess
index.php
application
controller
model
view
config/
    routes.php
error/
    error.php

Inside application/config/routes.php I have the following code:

$route['default_controller'] = "MyController";

Now what I am trying to achieve is when any user visits my root directory using browser I want to get the value of $route['default_controller'] from route.php file and load the php class inside the folder controller that matches with the value .

And also if any user tries to visit my application using an url like this: localhost/mvc/cars, I want to search the class name cars inside my controller folder and load it. In case there is no class called cars then I want to take the user to error/error.php

I guess to achieve the above targets I have to work with the .htaccess file in the root directory. Could you please tell me what to code there? If there is any other way to achieve this please suggest me.

I have tried to use the .htaccess codes from here, but its not working for me

tereško
  • 58,060
  • 25
  • 98
  • 150
black_belt
  • 6,601
  • 36
  • 121
  • 185
  • I think your answer is here? http://stackoverflow.com/questions/5280347/autoload-classes-from-different-folders – Steven May 04 '13 at 08:10
  • @Steven Thanks for your reply. I have visited the link, it answers how to auto load classes, but I have a bit different issue here. :) – black_belt May 04 '13 at 08:17
  • I just don't understand why you are dragging the .access file into this (I might simply just not understand what you are trying to do).All you want to do is to load classes, right? You "root folder" is your index.php file. Index.php will load data from routes.php. If you access www.b.com/mvc/cars it still loads index.php, but it knows the URL is /mvc/cars. – Steven May 04 '13 at 08:41

1 Answers1

4

It all sounds well and good from a buzzword standpoint, but to me this is all a little confusing because I see PHP's model as an MVC model already. It's providing the API for you to program with and deliver your content to your web server Apache and your database (something like MySQL). It translates the code(model) for you into HTML(view) ... provided that's what you intend, and you're supplying code as the user input (control). Getting too wrapped up in the terminologies gets a little distracting and can lead to chaos when you bring someone in to collaborate who isn't familiar with your conventions. (This should probably never be used in a production environment for a paying gig.)

I can tell you that on the page that you referenced they guy's .htaccess file needs a little work. The [L] flag tells mod_rewrite that this is the last command to process when the rule returns true. So you would either need to do this:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^(.*)$    public/$1    [L]
 </IfModule>

Or the following... but he was using a passthru flag which means that he is implying there are other things that could be processed prior to the last rule (eg. might be rewrite_base or alias), but that's not actually the case with his .htaccess file since it's a little bare. So this code would work similar to the code above but not exactly the same. They can't be used together though, and really there would be no need to:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) index.php?url=$1
 </IfModule>

The difference is the in the way it's processed. On the first .htaccess example you're passing any file to index.php regardless of whether it exists or not. You can [accidentally] rewrite a path that has a real file so that the real file is never accessed using this method. An example might be you have a file called site.css that can't be accessed because it's being redirected back to index.php.

On the second ruleset he's at least checking to see if the server doesn't have a file or a directory by the name being requested, then they're forwarding it to index.php as a $_GET variable (which seems a little pointless).

The way I typically write these (since I know mod_rewrite is already loaded in the config) is to to this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* index.php

In my PHP code I pull the $_SERVER['REQUEST_URI'] and match it against a list of URIs from the database. If there's a match then I know it's a real page (or at least a record existed at some point in time). If there's not a match, then I explode the request_uri and force it through the database using a FULLTEXT search to see what potentially might match on the site.

Note: if you blindly trust the request_uri and query the database directly without cleaning it you run the risk of SQL injection. You do not want to be pwnd.

<?php
  $intended_path = $_SERVER['REQUEST_URI'];

  if(in_array($intended_path,$uris_from_database)){
    //show the page.
  } else {
    $search_phrase = preg_replace('!/!',' ',$intended_path);
    $search_phrase = mysqli_real_escape_string($search_phrase);
    $sql = "SELECT * FROM pages WHERE MATCH (title,content) AGAINST ('$search_phrase');"
  }    

Sorry if this sounds a bit pedantic, but I've had experience managing a couple of million dollar (scratch) website builds that have had their hurdles with people not sticking to a standard convention (or at least the agreed upon team consensus).

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35