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