SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Framework from an alias

    I'm learning the Zend Framework and I setup my server in two ways.

    One way was from a virtual host. Everything worked correctly, I added a custom controller for testing and it worked fine.

    Now, when I try to run from an alias (http://localhost/test) I get this error:

    Code:
    Fatal error: Uncaught exception 'Zend_Controller_Action_Exception' 
    with message 'Cannot redirect because headers have already been sent.' 
    in C:\test\library\Zend\Controller\Action.php:192 Stack trace: #0 
    C:\test\application\controllers\IndexController.php(15): 
    Zend_Controller_Action->_redirect('/test') #1 
    C:\test\library\Zend\Controller\Action.php(118): 
    IndexController->noRouteAction() #2 
    C:\test\library\Zend\Controller\Dispatcher.php(185): 
    Zend_Controller_Action->run(Object(Zend_Controller_Dispatcher), 
    Object(Zend_Controller_Dispatcher_Token)) #3 
    C:\test\library\Zend\Controller\Dispatcher.php(136): 
    Zend_Controller_Dispatcher->_dispatch(Object(Zend_Controller_Dispatcher_Token),
     true) #4 C:\test\library\Zend\Controller\Front.php(254): 
    Zend_Controller_Dispatcher->dispatch(Object(Zend_Controller_Dispatcher_Token))
     #5 C:\test\library\Zend\Controller\Front.php(106): 
    Zend_Controller_Front->dispatch() #6 C:\test\document_root\index.php(6): 
    Zend_Controller_Front::run('C:/test/applica...') #7 {main} thrown in 
    C:\test\library\Zend\Controller\Action.php on line 192
    Now, this is from the IndexController which was taken from the Zend Framework manual:

    Code:
    <?php
                    
    require_once 'Zend/Controller/Action.php';
    
    class IndexController extends Zend_Controller_Action 
    {
        public function indexAction()
        {
            echo 'Hello from IndexController';
        }
    
        public function noRouteAction()
        {
    		$this->_redirect('/');
    	}
    }
    
    ?>
    Again, when I run this as a virtual host (test.fwork.com), everything works fine. This error only occurs when I'm running as a host.

    Anyone have any ideas on this and how to fix it?

  2. #2
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's not Zend...

    Your tring to change the headers after you have started to send output to the user.
    Your old host had buffering on probably.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali
    it's not Zend...

    Your tring to change the headers after you have started to send output to the user.
    Your old host had buffering on probably.
    My old host? I'm not sure what you mean. This is all happening on my local machine.

    And I checked my apache httpd.conf for "buffering" and I'm not finding anything.

    I'm not trying to send anything to the client, I have the most barebones Zend Framework app there is so I can get the basics down. Zend Framework must be sending the headers, because there is no other code in the app beside this index controller and my Foo controller.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well this is a kick in the teeth.

    The check to headers_sent() is occuring in Zend_Controller_Action::redirect method.

    Now, you can pass in variables to headers_sent ( headers_sent($filename,$linenum) ) to find out when the headers were written, and it's complaining about the index.php page!

    It's saying that my
    Code:
    <?php
    tag in the index.php page is causing headers to be written to the page. How the heck do I get around that??

  5. #5
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    prob a space before <?php ?

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali
    prob a space before <?php ?
    I think that was it! Not a space, but a blank line. Also some extra lines after ?> in the IndexController.

    Thanks! Now, just one more problem

    My RewriteBase doesn't seem to be working. Here's my .htaccess:

    Code:
    RewriteEngine on
    RewriteBase /test
    RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
    php_value include_path "C:/test/library"
    Now, the index controller says to redirect to /:

    Code:
        public function noRouteAction()
        {
    		$this->_redirect('/');
        }
    But when I hit my alias (http://localhost/test), I get redirected to http://localhost

    How should I handle the redirect for an alias? If I make it $this->redirect('/test'); it loops and fails to ever get to the index action..??

    BTW - I'm using Apache 2.0.55 and PHP 5.1.6, framework version is 0.1.5

    Thanks again for the help - I'm almost there.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - check this out.

    I moded my Zend_Controller_Router class with a way that works, but is very hard-coded. I edited this class because it's in the router that the controller and action are parsed out of the URI:

    This is the pertinent portions of the route method I modified.
    Code:
    // Add flag to determine if this is an alias
    $alias = true;
    
    // Begin alias support mod
    if ($alias) {
    	$controller = $path[1];
    	$action     = isset($path[2]) ? $path[2] : null;
    } else {
    	/**
    	 * The controller is always the first piece of the URI, and
    	 * the action is always the second:
    	 *
    	 * http://zend.com/controller-name/action-name/
    	 */
    	$controller = $path[0];
    	$action     = isset($path[1]) ? $path[1] : null;
    }
    Now I know this is weak code and is not flexible. The alias is assumed to only be one level from the root, IE http://localhost/test. But it does work.

    Is there a better way to do this? Or should I just bail on the whole using an alias idea and move my code into a virtual host?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •