Zend Framework & Infinite Redirecting

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
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Zend Framework & Infinite Redirecting

Post by Hades »

I'm trying to use the Zend Framework version 0.9.1. Problem is that any controller other than index gives me a message in firefox saying that the page isn't redirecting properly.

I'm using the .htaccess redirect from the ZF manual and there are no header or other redirects in the code.

I'm really stumped by this. Any ideas ?

Thanks in advance :)
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Is your .htaccess trying to redirect to a file in the same directory? If so, i would assume that the .htaccess would get processed again and it would keep redirecting.
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

I don't understand... AFAIK rewrite rules for Zend Framework forward everything to index.php apart from images, css and javascript files.

The index.php has to be in the web-root, and so does the .htaccess in order for this to work as described in the ZF manual.

The IndexController in my setup works fine... it's just any other controller that barfs. Even if all it is set to do is output a view.
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

index.php

Code: Select all

<?php
/**
 * Bootstrap file for HUB Application
 *
 * For ZF 0.9.1 including incubator
 */

/*
 * The basics...
 */
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1); //disable on production servers!
date_default_timezone_set('Europe/London');

/*
 * Setup the include_path to the ZF library.
 * We set the incubator first so the
 * incubator classes are loaded in preference
 * to core ZF classes where two versions exist.
 */
$include_path = '/home/editpc/hubdata/library/incubator/library'.PATH_SEPARATOR.'/home/editpc/hubdata/library';
set_include_path($include_path);

/*
 * Include the Zend_Loader
 */
require_once 'Zend/Loader.php';

/*
 * Autoload magic php function... avoids the need to include class files manually.
 * This will also be available in all files included beyond this point.
 *
 * In this file Zend_Loader will be used... elsewhere __autoload will be used.
 */
function __autoload($class_name){
	if(!class_exists($class_name,false)){
		$class_path = str_replace('_',DIRECTORY_SEPARATOR,$class_name);
		require_once($class_path.'.php');
	}
}

/*
 * Get the config data from our config.ini file and store in registry
 */
Zend_Loader::loadClass('Zend_Config_Ini');
$config = new Zend_Config_Ini('/home/editpc/hubdata/data/config.ini', 'staging');
Zend_Registry::getInstance()->set('config', $config);

/*
 * Load and register our View for later use
 */
Zend_Loader::loadClass('Zend_View');
$view = new Zend_View();
$view->setScriptPath($config->base_path.'application/views');
Zend_Registry::getInstance()->set('view', $view);

/*
 * Load and register our database for later use.
 */
$params = array ('host'     => $config->database->host,
                 'username' => $config->database->username,
                 'password' => $config->database->password,
                 'dbname'   => $config->database->name);

Zend_Loader::loadClass('Zend_Db');
$db = Zend_Db::factory($config->database->type, $params);
Zend_Registry::getInstance()->set('db', $db);

/*
 * Instantiate a Request to set BaseURL
 */
Zend_Loader::loadClass('Zend_Controller_Request_Http');
$request = new Zend_Controller_Request_Http();
$request->setBaseUrl($config->base_url);

/*
 * Setup and run the Front Controller
 *
 * Set Controller Dir, dispatch the modified Request (with updated BaseURL) and finally
 * get the resulting Response object.
 */
$controller = Zend_Controller_Front::getInstance();
$controller->throwExceptions(true);
$controller->returnResponse(true);
$controller->setControllerDirectory($config->base_path.'application/controllers');
$response = $controller->dispatch($request);

/*
 * By default Exceptions are not displayed
 * That won't do during development.
 * Remove this in a live environment!
 *
 * $response->renderExceptions(true); will not
 * work, it's a bit broken and was fixed in SVN for
 * next release. Until then...manually echo exception
 */
if($response->isException())
{
    echo $response->getException();
    exit; // Stop here - ;)
}

/*
 * Echo the response (with headers) to client
 * Zend_Controller_Response_Http implements
 * __toString().
 */
echo $response;
?>
IndexController.php - This works

Code: Select all

<?php
/**
 * HUB Application IndexController
 *
 * This is the indexController for the HUB Application.  It will handle all
 * requests to the main index of the application.
 *
 * @category   HUB
 * @package	controller
 * @copyright  Copyright (c) 2007 Lee Conlin
 */

class IndexController extends Zend_Controller_Action
{

	public function IndexAction()
	{
		/*
		 * Get View from Registry
		 */
		$view = Zend_Registry::getInstance()->get('view');

		/*
		 *Assign view data
		 */
		$view->page_title = ':: Index';

		/*
		 * Lets use a Response object to collate
		 * any output.
		 * No need to set headers unless output is
		 * not text/html (PHP default)
		 */
		$this->getResponse()->setBody(
			$view->render('index.tpl.php')
		);
	}
}
?>
LoginController - This gives me the error

Code: Select all

<?php
/**
 * HUB Application LoginController
 *
 * This is the LoginController for the HUB Application.  It will handle all
 * login and user management.
 *
 * @category   HUB
 * @package	controller
 * @copyright  Copyright (c) 2007 Lee Conlin
 */

class LoginController extends Zend_Controller_Action
{

	public function indexAction()
	{
		/*
		 * Get View, DB and config from Registry
		 */
		$view = Zend_Registry::getInstance()->get('view');
		$db = Zend_Registry::getInstance()->get('db');
		$config = Zend_Registry::getInstance()->get('config');

		/*
		 *Assign view data
		 */
		$view->page_title = ':: Login';
		$view->formAction = $config->base_url.'login/login';

		/*
		 * Lets use a Response object to collate
		 * any output.
		 * No need to set headers unless output is
		 * not text/html (PHP default)
		 */
		$this->getResponse()->setBody(
			$view->render('login_form.tpl.php')
		);
	}
}
?>
.htaccess

Code: Select all

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

php_flag register_globals off
php_flag magic_quotes off
I hope someone can see what I'm doing wrong cos it's driving me mad :roll:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Does "indexAction" need to be capitalized?
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

Have tried it capitalisied and lowercase and still the same problem.

Some more troubleshooting has found that all controllers seen to be redirecting to IndexController->IndexAction.

Degug output on $request in index.php shows...

Code: Select all

object(Zend_Controller_Request_Http)#7 (13) {
  ["_requestUri:protected"] => string(6) "/login"
  ["_baseUrl:protected"] => string(27) "http://editpc.servehttp.com"
  ["_basePath:protected"] => NULL
  ["_pathInfo:protected"] => string(0) ""
  ["_params:protected"] => array(0) {
  }
  ["_aliases:protected"] => array(0) {
  }
  ["_dispatched:protected"] => bool(false)
  ["_module:protected"] => NULL
  ["_moduleKey:protected"] => string(6) "module"
  ["_controller:protected"] => NULL
  ["_controllerKey:protected"] => string(10) "controller"
  ["_action:protected"] => NULL
  ["_actionKey:protected"] => string(6) "action"
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what are $config->base_path and $config->base_url?
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

base_path is the file path to the directory that contains "application", "data" and "library"

base_url is the URL to the web-root.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

no no I know what they are... I'm sorry I should have been more clear. Can you post what they are as in:

base_url: http://localhost
base_path: /home/htdocs/
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

Sorry... here they are

base_url is "http://editpc.servehttp.com/"

base_path is "/home/editpc/hubdata/"

Edit... Ok... I figured it out.

I was instantiating the Zend_Request_Http class manually and passing it to the Front Controller... it seems as though it wasn't setting the Controller or Action properties on the request object...

By allowing the Front Controller to create it's own request object it all started working properly.
Post Reply