Page 1 of 1

Needle in haystack problem

Posted: Sat Jul 25, 2015 1:01 am
by Mr-kumar
I was developing some dummy controllers, for faster developments. But can't figure out what is the probem with my pipes . It can't find the array key when the controller is post.

Code: Select all

<?php
	function call($controller, $action) {
		// taking the file which matches with the controller name.
		require_once('controllers/'. $controller . '_controller.php');

		switch ($controller) {
			case 'page':
				$controller = new PageController();
				break;
			case 'post':
				//to query database later in the controller
				require_once('models/post.php'); 
				$controller = new PostController();
				break;

		}

		$controller->{$action}();
	}

	// list of controllers and their actions..
	// considering those "allowed" values..

	$controllers = array('page' => ['home', 'error'],
						 'post' => ['index', 'show']);
	

	// checking if requested controller and actions are both allowed..
	if(array_key_exists($controller, $controllers)) {
		if(in_array($action, $controllers[$controller])) {
			call($controller, $action);
		}else {
			call('page', 'error');
		}
	}else {
			call('page', 'error');
		}

	// if someone tries to access something else he/she will be redirected to error landing page..
?>
The full repository is here. https://github.com/shadow-stranger/dumm ... tree/posts

Any help is appreciated.

Re: Needle in haystack problem

Posted: Sat Jul 25, 2015 1:33 am
by requinix
Looks fine. Are you absolutely sure the value is "post" and the action is "index" or "show"? What does

Code: Select all

var_dump($controller, $action);
show?

Re: Needle in haystack problem

Posted: Sat Jul 25, 2015 12:31 pm
by Mr-kumar
Thanks , the variable was "posts" and not "post" in this file :mrgreen:
in file display/post/index.php

Code: Select all

<p> Here is the list of all the posts </p>

<?php foreach ($posts as $post) {
	?>
	<p> <?php echo $post->author; ?>
		<a href='?controller=posts&action=show&id=<?php echo $post->id; ?>'> See post </a>
	</p> <?php
} ?>