.htaccess routing and mvc

Before I start to learn a framework like Laravel, I’m doing some learning on how a mvc framework should work. This is my file structure on my localhost

  • app (this filder contain the model, view etc)
  • public (this folder contain the index.php file and template files)

In the index.php I’ve got the following code

defined('KM_APPLICATION_PATH') || define('KM_APPLICATION_PATH',realpath(dirname(__FILE__) . '/../app'));
defined('KM_DS') || define('KM_DS', DIRECTORY_SEPARATOR);
require_once KM_APPLICATION_PATH . KM_DS . 'km-config' . KM_DS . 'km-config.php';
$km_page  = km_get_url('km-page','home');
$km_models = KM_MODELS_PATH . $km_page . '.php';
$km_views  = KM_VIEWS_PATH . $km_page . '.php';

if(file_exists($km_models)){

    require_once $km_models;

}else{

    require_once KM_ERROR_404;

}

if(file_exists($km_views)){

    $main_content = $km_views;

}else{

    require_once KM_ERROR_404;

}

include KM_VIEWS_PATH. 'layout.php';

and this is the function km_get_url

function km_get_url($km_page, $km_default_page=''){

    if(isset($_REQUEST[$km_page])){
        return $_REQUEST[$km_page];
    }else{
        return $km_default_page;
    }

}

I’ve created a controller and view for login page and it works fine if call it in the url as www.mysite.com/?km-page=km-login

How can i use .htaccess to make the url looking nicer for exmaple www.mysite.com/km-login

I’ve tried with

<IfModule mod_rewrite.c>    
RewriteEngine on
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?km-page=$1 [L,QSA]

</IfModule>

But i get a php notice error saing PHP Notice: Undefined variable: main_content and also include(): Filename cannot be empty inside the layout.php file where i call the variable $main_content. I believe it is because the function km_get_url doesn’t work properply maybe?

Do you have a solution to this problem? Many thanks

The easiest way to know what’s going wrong is by checking what data your application is using and see if it matches your expectations.

To that I end I would suggest add var_dump($_GET); as the very first line in your index.php, then visit the site and check the value of km-page.

You can do some of this with htaccess but it is rather limited and you end up being tied to apache. This might be a good time to introduce a router component. Router’s can give you pretty url’s using php only. And if you really want then you can write your own just to see how they work.

I usually create the following DEBUG function in my index.php file because once created it is easier to type than var_dump(…)


/* 
  usage: 
    fred( $val );  // print_r($val ) and styled
    fred( $val, tru3 );  // var_dump($val ) and styled
*/
function fred($val=NULL, $vd=FALSE)
{
// PRE adds linefeeds and styling to 
echo '<pre style="width:88%; margin:1em auto; background-color:#ff0; color: #000; text-align:left: padding:0.42em;">';
if($vd):
  var_dump($val);
else:
  print_r($val);
endif;
echo '</pre>';

}///

Also useful for tracing is `echo __LINE__; die;` // halts exection

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.