Page 1 of 1
[Solved] ZendFramework MVC How to Set Controller ?
Posted: Thu Sep 28, 2006 3:02 am
by CoderGoblin
Hello All,
Decided to look at the Zend Framework again having left it alone since the first version. I have been trying to get things set up for an MVC following the tutorial at
akrabat. The difficultly I have is that I cannot get any "controller" to work other than IndexController. In the bootstrap file I have...
Code: Select all
$route = new Zend_Controller_RewriteRouter();
$route->addRoute('edit', ':controller/:action/id/:id', array('controller' =>'index', 'action' => 'index'));
$controller = Zend_Controller_Front::getInstance();
$controller->setRouter($route);
// run
$controller->run('./application/controllers');
I have tried all sorts of combinations for the addRoute with no success so I put it back to what the tutorial had (tutorial didn't try to implement a different controller).
Any help appreciated . If I cannot get this working the investigation of this framework will halt for some more versions.
Posted: Thu Sep 28, 2006 10:00 am
by Luke
This is what one of my bootstrapper files looks like (index.php). It works fine. It looks like you set up your controller the same way I do, so I think it might be a problem with your actual controller files... can you post your index controller and another controller so I can see if you made those correctly?

Also... post your .htaccess file as well...
Code: Select all
<?php
error_reporting(E_ALL);
date_default_timezone_set('America/Los_Angeles');
require_once 'library/functions.php';
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
set_include_path(get_include_path() . PS . '.' . DS . 'library' . DS . PS . '.' . DS . 'application' . DS . 'models');
require_once "Zend.php";
Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_Controller_RewriteRouter');
Zend::loadClass('Zend_View');
Zend::loadClass('Zend_Config');
Zend::loadClass('Zend_Config_Ini');
Zend::loadClass('Zend_Log');
// load configuration
$config = new Zend_Config(Zend_Config_Ini::load('./application/config.ini', 'general'));
Zend::register('config', $config);
// setup database
require_once('adodb/adodb.inc.php');
require_once('adodb/adodb-active-record.inc.php');
$db = NewADOConnection($config->db->dsn);
//$db->debug = true;
ADOdb_Active_Record::SetDatabaseAdapter($db);
Zend::register('db', $db);
// Load Application model & controller
require_once 'application/appModel.php';
require_once 'application/appController.php';
// Initialize Logger
require_once 'Zend/Log.php'; // Zend_Log base class
require_once 'Zend/Log/Adapter/File.php'; // File log adapter
// Register the file logger
Zend_Log::registerLogger(new Zend_Log_Adapter_File('logs/errors.txt'));
// Prepare view and set defaults
$view = new Zend_View();
$view->setScriptPath('./application/views');
$view->defaults = $config->view->asArray();
$view->url = geturl();
$view->headercontent = '';
$view->bodytag = "<body>";
// register the view we are going to use
Zend::register('view', $view);
// setup User Session
$Session = new Session('User');
Zend::register('Session', $Session);
// setup controller
$route = new Zend_Controller_RewriteRouter();
/*id/:id*/ // This was in the router, but I took it out
$route->addRoute('user', ':controller/:action/id/:id', array('controller' => 'index', 'action' => 'index'));
$route->setRewriteBase('/dev');
$controller = Zend_Controller_Front::getInstance();
$controller->setRouter($route);
// and we're off!
$controller->run('./application/controllers');
?>
This is my htaccess file. It's in the same directory as the bootstrapper file...
Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
</IfModule>
Posted: Mon Oct 02, 2006 4:17 am
by CoderGoblin
Sorry going to be a long one...
Index Controller-php in (application/controllers):
Code: Select all
<?php
class IndexController extends Zend_Controller_Action
{
function IndexAction()
{
$view= Zend::registry('view');
$view->title = "Start Page";
$view->actionTemplate = 'indexIndex.tpl.php';
echo $view->render('site.tpl.php');
}
function testAction()
{
echo '<p>in '.__CLASS__.'->'.__FUNCTION__.'</p>';
}
public function __call($m, $a)
{
print_r($this);
echo("Bad Boy");
//$this->_redirect('/');
}
}
?>
InsertController.php in application/controllers
Code: Select all
<?php
class InsertController extends ZendControllerAction
{
function indexAction()
{
echo '<p>in '.__CLASS__.'->'.__FUNCTION__.'</p>';
}
function editAction()
{
echo '<p>in '.__CLASS__.'->'.__FUNCTION__.'</p>';
}
function statistikAction()
{
echo '<p>in '.__CLASS__.'->'.__FUNCTION__.'</p>';
}
public function __call($m, $a)
{
print_r($this);
echo("Bad Boy");
//$this->_redirect('/');
}
}
?>
index.php
Code: Select all
<?php
session_start();
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Europe/Berlin');
set_include_path('.'.PATH_SEPARATOR.'./library'.PATH_SEPARATOR.'./application/models'.PATH_SEPARATOR.'./views');
include "Zend.php";
Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_Controller_RewriteRouter');
Zend::loadClass('Zend_View');
Zend::loadClass('Zend_Config');
Zend::loadClass('Zend_Config_Ini');
Zend::loadClass('Zend_Db');
// load configuration
$config = new Zend_Config(Zend_Config_Ini::load('./application/config.ini','general'));
Zend::register('config',$config);
// setup database
//$db = Zend_Db::factory($config->db->adaptor,$config->db->config->asArray());
// register the view we are going to use
$view = new Zend_View();
$view->setScriptPath('./application/views');
Zend::register('view',$view);
// setup controller
$route= new Zend_Controller_RewriteRouter();
$route->addRoute('admin',':controller/:action', array('controller'=>'index','action'=>'index'));
$controller = Zend_Controller_Front::getInstance();
$controller->setRouter($route);
// run
$controller->run('./application/controllers');
// close tag not required and may cause errors
//
The start page works, as does /index/test but /insert doesn't and comes up with 404 error.
It should be noted that I am playing on a machine where the path is "
http://machine/~myuser/misc/zf_mvc/" and I think this is what is causing the problem. I have tried setting RewriteBase in both the .htaccess and also the index.php file with no luck. I have also browsed for solutions, with no solution after 0.1.2 where it should work.
Posted: Mon Oct 02, 2006 5:26 am
by Jenk
use first word lowercase (e.g. indexController) for class names.
then using
http://www.example.com/index/index/
Will trigger indexController::indexAction(); (non-statically)
So to create a different controller; called testController for example;
Code: Select all
<?php
class testController extends Zend_Controller_Action
{
public function testAction()
{
echo 'This is a test.';
}
public function indexAction()
{
echo 'indexAction triggered';
}
}
?>
Then use
http://www.example.com/test/test to see results.
[Solved] ZendFramework MVC How to Set Controller ?
Posted: Mon Oct 02, 2006 5:34 am
by CoderGoblin
Eek,
After looking around for ages, the solution was so simple. It was a typo in the naming of the .htaccess file (previously .htacess) hence not calling the index.php and showing the 404.
I feel so stupid for not spotting it.

Sorry for wasting your time guys