Jump to content
Killersites Forums

Issue with setting up controller (MVC)


Recommended Posts

Hi, Just started trying some MVC organisation with PHP but I think theres something wrong with my .htaccess since i get this error:

 

"The server encountered an internal error or misconfiguration and was unable to complete your request.

 

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

 

More information about this error may be available in the server error log."

 

My htaccess looks like this:

RewriteEngine On

RewriteBase /test_mvc/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [QSA.L]

 

I also have these files and classes:

index.php

 

<?php
// define site url
define("BASE_PATH", "http://localhost");

// define our base path
$path = "/my_mvc";

// Take the initial PATH.
$url = $_SERVER['REQUEST_URI'];
$url = str_replace($path,"",$url);

//make array from rest or url
$array_tmp_uri = preg_split('[\\/]',$url -1, PREG_SPLIT_NO_EMPTY);

//Here, we will define what is what in the URL
$array_uri['controller'] 	= $array_tmp_uri[0]; //a class
$array_uri['method']	= $array_tmp_uri[1]; //a função
$array_uri['var']		= $array_tmp_uri[2]; //a variavel

//Load base
require_once("application/application.php");

//Load controller object
$application = new Application($array_uri);
$application = new Application($array_uri['controller']);



?>

 

application class

 

<?php
class Application {
var $uri;
var $model;	

function __construct($uri) {
	$this->uri = $uri;	
}

function loadController($class) {
	$file = "application/controller/".$this->uri['controller'].".php";	

	if(!file_exists($file)) die();

	require_once($file);

	$controller = new $class();

	if(method_exists($controller, $this->uri['method'])) {
		$controller->{$this->uri['method']}($this->uri['var']);
	}else{
		$controller->index();	
	} 	

}

function loadView($view,$vars="") {
	if(is_array($vars) && count($vars) > 0)
		extract($vars, EXTR_PREFIX_SAME, "wddx");
	require_once('view/'.$view.'.php');	
}

function loadModel($model) {
	require_once('model/'.$model.'php');
	$this->$model = new $model;	
}

}





?>

the class that loads views.

 

<?php
class Blog extends Application {
	function __construct() {
		$this->loadModel('model_blog');	
	}

	function index() {
		$articels = $this->model_blog->select();
		$data['articles'] = $articles;
		$this->loadView('view_blog',$data);		
	}

	function add($title="") {
		$data['title'] = $title;
		$this->loadView('view_blog_add',$data);	

	}


 }

?>

 

 

I have hardly any idea of whats going on here. Im just learning how to get the controller to pass link the right view.

 

Thanks :)

Link to post
Share on other sites

Based on your error, it seems like the issue is currently with your .htaccess file, and doesn't have anything to do with your code. Perhaps try using this RewriteRule line, rather than your current line within your .htaccess file?

 

RewriteRule ^(.*)$ /index.php/$1 [L]

Why do you have the QSA in your line? I believe that means "query string append" and I don't believe that makes sense in this case? Also, I'm pretty sure you need a comma between the flags within the brackets ("[QSA, L]") rather than a period.

 

I'm pretty sure you could do a Google search or two for "rewrite everything to index.php mod rewrite" and you will get some examples to work from. For example, here's one: http://stackoverflow.com/questions/6589995/rewrite-everything-to-be-after-index-php

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...