Needle in haystack problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mr-kumar
Forum Newbie
Posts: 3
Joined: Sat Jul 25, 2015 12:44 am

Needle in haystack problem

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Needle in haystack problem

Post 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?
Mr-kumar
Forum Newbie
Posts: 3
Joined: Sat Jul 25, 2015 12:44 am

Re: Needle in haystack problem

Post 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
} ?>
Post Reply