Page 1 of 1

Zend default routing

Posted: Fri Dec 11, 2009 1:35 pm
by alex.barylski
I have the following code:

Code: Select all

<?php
 
  chdir('core/libraries');
 
  session_start();
  session_regenerate_id();
 
  spl_autoload_register('mypro_autoload');
 
  Zend_Registry::set('config', $config);
  $front = Zend_Controller_Front::getInstance();
 
  $front->registerPlugin(new MyPro_Controller_Plugin_CheckAuth());
  $front->registerPlugin(new MyPro_Controller_Plugin_NoCache());
 
  $front->setParam('noViewRenderer', true);
  //$front->setParam('noErrorHandler', true);
  //$front->setParam('useDefaultControllerAlways', true);
 
  //$front->setDefaultControllerName('Index');
  $front->setDefaultAction('index');
  $front->setDefaultModule('domain');
 
  $front->setModuleControllerDirectoryName('controllers');
  $front->addModuleDirectory('../../module/system');
  $front->addModuleDirectory('../../module/plugin');
 
  $front->dispatch();
My modular file structure looks like this:

Code: Select all

admin/
  index.php
  module/
    plugin/
      shipping/
        controllers/
          IndexController.php
    system/
      domain/
        controllers/
          IndexController.php  
 
When I enter the following URI(s) everything works as expected:

Code: Select all

 
admin/
admin/domain
admin/domain/index
 
// These fail!!!
 
admin/shipping
 
The shipping module is under the 'plugin' whereas domain is under 'system' yet I have tried moving 'shipping' into 'system' and the same error happens.

The domain module controller is properly routed but every other module is not being dispatched to instead I get a exception of the form:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php:242 Stack trace: #0 C:\wamp\www\admin\core\libraries\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\wamp\www\admin\core\bootstrap.php(28): Zend_Controller_Front->dispatch() #2 C:\wamp\www\admin\index.php(11): include('C:\wamp\www\adm...') #3 {main} thrown in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php on line 242
Without re-writing a custom router, how do I get routes to dispatch to the proper modules in the directory structure provided above. I stopped testing once I got the domain module to start responding and now this, I'm totally lost with Zend.

Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 2:03 pm
by Eran
There are too many things that could go wrong here, and since you didn't implement the error controller you can't tell exactly what is the error you are encountering.

My guess is you didn't add the proper prefix to the controller classes in the 'domain' controllers. Any controller besides the default module need to be prefixed with the module name, such as:

Code: Select all

class Domain_IndexController extends ...

Re: Zend default routing

Posted: Fri Dec 11, 2009 2:32 pm
by alex.barylski
First off, thanks for replying. I spent a few weeks promoting Zend as a better solution to CodeIgnitor and now I've run into routing issues, everyone is start to question my abilities and Zend itself. So hopefully i figure out a hack just to get moving forward. :P
My guess is you didn't add the proper prefix to the controller classes in the 'domain' controllers. Any controller besides the default module need to be prefixed with the module name, such as:
I wasn't prefixing the controller classes and when I did as you suggested, voila, problem solved. Phew!!!

I owe you beer brother, one day :)

Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 2:34 pm
by alex.barylski
I'm expericing anothe rissue now. :banghead:

While the default indexAction() of modules is being dispatched any other action I implement, such as manageShowAction is not being invoked...

Code: Select all

admin/shipping
admin/shipping/index => WORKS
 
admin/shipping/test => FAILS
Any ideas? Do I need to prefix actions with the module name as well? Do you know where I might docs for this???

Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:10 pm
by Eran
showing some code would be helpful.. you wrote both 'manageShow' and 'test' (in your route example), so which one is it? note that you can't use camelCase in action names - invoked actions from the URL are lower-cased, so they can't contain any upper case letters.

By the way, which version of ZF are you using? your bootstrap doesn't seem to include the conventions used in recent versions.

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:19 pm
by alex.barylski
showing some code would be helpful.. you wrote both 'manageShow' and 'test' (in your route example), so which one is it?
What would you need to see? To answer your question, I tried both:

Code: Select all

indexAction() => admin/index => works (with or without 'index)
testAction() => admin/test => Breaks with exception
note that you can't use camelCase in action names - invoked actions from the URL are lower-cased, so they can't contain any upper case letters.
You can't? I've use this convention on every method so far...what is the convention then? IndexAction? Why would indexAction() be dispatch properly???

EDIT | Just realized your talking about the URI not the method names...LOL. I knew about this from my last experience with Zend but we used a routing file declared in XML and it wasn't built for modularity so a custom modules directory wasn't need either.
By the way, which version of ZF are you using? your bootstrap doesn't seem to include the conventions used in recent versions.
I'm using the latest version, 1.9ish. We implemented out own bootstrap, no reason in particular, just to see how everything fits togather I guess.

Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:26 pm
by Eran
testAction() => admin/test => Breaks with exception
what is the exception..?
and show the controller code

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:30 pm
by alex.barylski
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php:242 Stack trace: #0 C:\wamp\www\admin\core\libraries\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\wamp\www\admin\core\bootstrap.php(30): Zend_Controller_Front->dispatch() #2 C:\wamp\www\admin\index.php(11): include('C:\wamp\www\adm...') #3 {main} thrown in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php on line 242
Same exception I always seem to get... :(

Here is the controller for shipping. My other controller(s) looks very much the same but the shipping module will not have any of it's methods, other than indexAction() dispatched. Even if I set the default module ot 'shipping'

Code: Select all

$front->setDefaultModule('shipping');
I don't get it...why would the one module (that has name spacing now as I set prefixDefaultModule to true on the $front object) properly have it's actions dispatched and the shipping module has it's index dispatched but no other method???

Code: Select all

<?php
 
  class Shipping_IndexController extends Zend_Controller_Action{
 
    private $child = null;
 
    public function preDispatch()
    {
      $this->child = new MyPro_View_Basic();
      $this->child->setScriptPath(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'views');
    }
 
    public function indexAction()
    {
       // This method executes fine the output is as expected
      $header = _T('Shipping Help');
      $message = _T('Welcome to Shipping module help information.');
 
      $this->getResponse()->setBody($this->buildView($header, $message, $this->child->render('index.phtml')));
    }
 
    public function testAction()
    {
       // This method doesn't execute and I cannot figure out why?
 
      $header = _T('Shipping Help');
      $message = _T('Welcome to Shipping module help information.');
 
      $this->getResponse()->setBody($this->buildView($header, $message, $this->child->render('index.phtml')));
    }
 
  }
Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:44 pm
by Eran
Same exception I always seem to get...
Since you still haven't created an error controller for your application. That's the most basic step so you could see the actual error you are encountering (you get 'error' controller not found since it is trying to load it to show you the actual error encountered).
http://framework.zend.com/manual/en/zen ... rorhandler

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:46 pm
by alex.barylski
Since you still haven't created an error controller for your application. That's the most basic step so you could see the actual error you are encountering (you get 'error' controller not found since it is trying to load it to show you the actual error encountered).
I"ll look into it, thanks :)

Re: Zend default routing

Posted: Fri Dec 11, 2009 3:56 pm
by alex.barylski
OK I added a ErrorController.php beside my IndexController.php under the 'domain' module directory.

Added the views/scripts/error/error.phtml view using the content provided by the example...why is it everytime I use Zend I feel like I'm stuttering? :lol:

Code: Select all

 
<?php
 
  class ErrorController extends Zend_Controller_Action{
 
    public function errorAction()
    {
 
    }
 
  }
 
I have also tried prefixing the ErrorController with Domain_ and I still get the exception error.

Is there any way I can get the information you are seeking by echo'ing the caught exceptions getMessage() method? I don't see much point in this error controller other than pretty-fying the error messages in an HTML layout that might match my design.

I'm not using Zend_Layout either so this might be causing an issue with the error...although I doubt that is affecting dispatching as I have never used Zend_Layout and never had this problem before.

Cheers,
Alex

Re: Zend default routing

Posted: Fri Dec 11, 2009 4:31 pm
by Eran
The error controller allows you to handle exception through the MVC chain. That includes showing an error screen (and not the default Fatal Error screen), logging, sending appropriate headers (404,500) and so forth. You can get the error handler inside the controller and output/log the errors. For example:

Code: Select all

 
public function errorAction() {
    $handler = $this->_getParam('error_handler'); 
    $errors = array(
            'message' => $handler -> exception -> getMessage(),
            'trace' => $handler -> exception -> getTraceAsString()
    );
    Zend_Debug::dump($errors);
}

Re: Zend default routing

Posted: Sat Dec 12, 2009 4:15 pm
by alex.barylski
I mucked around with the error controler for a few hours and couldn`t get anything working so I scrapped that idea. I followed the file naming convention-structure but nothing I did worked, thankfully I figured out what the issue was and resolved the routing problem.

Thanks for all your help though dude, you`ve been very helpful and insightful, and I appreciate your taking the time to assist me in working with Zend - I hate the framework as is and without your valued experience I would probalby have sent hate mail to Zend or something. LOL. I`m kidding of course - about the hate mail... :)

Cheers,
Alex

Re: Zend default routing

Posted: Sat Dec 12, 2009 4:31 pm
by Eran
What problems did you encounter? setting up the error controller should be very straightforward. Just create a file name ErrorController inside a directory called '/error' in your default module directory. inside put the error controller as specified in the link that I posted previously.

You should really take care of this, as it's the standard way to observe exceptions in the framework now. How can you debug your application without seeing the actual error messages?