Rendering View from extended Base class in another module

Hi everyone,

I have found some pieces of code that use a "shared" view but that is not exactly what I am after. Does anyone have piece of code that allows views to be rendered based on extended classes.

For example:

    Modules/
        - Base/
            Views/
                Index/
                    - index.phtml

        - Admin
            Views/
                Index/
                    -

So in the example above if I am currently running in an "Admin" controller which extends a Base controller and the view doesnt exist in the admin module that it will go one module down to check if the view exists there?

So this could go multiple levels, Admin module extends Moderator module which extends base Module.

Anyone have any suggestions as to how this would best be approached?

Are you setting up a view component in your Module.php file?

    /**
     * Register specific services for the module
     */
    public function registerServices($di){

        //Registering a dispatcher
        $di->set('dispatcher', function() use (&$di) {
            # ... code here
            return $dispatcher;
        });

        //Setting up the view component
        $di->set('view', function() {

            $view = new View();

            $view->setViewsDir(MODULE_DIR . "admin" . DS . "views" . DS);

            $view->registerEngines([
                '.volt' => function($view, $di) {

                    $volt = new VoltEngine($view, $di);

                    $volt->setOptions([
                        'compiledPath' => APP_DIR.'cache'.DS,
                        'compiledSeparator' => '_'
                    ]);

                    return $volt;
                },
                '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
            ]);

            return $view; 
        }, true);
    }

See also: http://docs.phalconphp.com/en/latest/reference/applications.html#multi-module

Thanks for the reply Andy!

Yes I am doing that. However at the moment that is restricting me to use the views that are ONLY within that module. If I have an admin controller that extends out of the Base module I would like to access the Base view if the admin view doesnt exist.

Are you setting up a view component in your Module.php file?

```php /** * Register specific services for the module */ public function registerServices($di){

   //Registering a dispatcher
 $di->set('dispatcher', function() use (&$di) {
     # ... code here
     return $dispatcher;
 });

 //Setting up the view component
 $di->set('view', function() {

     $view = new View();

     $view->setViewsDir(MODULE_DIR . "admin" . DS . "views" . DS);

     $view->registerEngines([
         '.volt' => function($view, $di) {

             $volt = new VoltEngine($view, $di);

             $volt->setOptions([
                 'compiledPath' => APP_DIR.'cache'.DS,
                 'compiledSeparator' => '_'
             ]);

             return $volt;
         },
         '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
     ]);

     return $view; 
 }, true);

}

``` See also: http://docs.phalconphp.com/en/latest/reference/applications.html#multi-module

Oh right well I don't have code to do that but I have encountered a similar situation where I needed the view from a different module; so I have a method in some base controller which looks like this:

        /**
         * Use admin_view() to force the admin layout and templates.
         */
        protected function admin_view() {
            $this->view->setViewsDir(MODULE_DIR . "other_module" . DS . "views" . DS);
            $this->view->setTemplateAfter('admin');
        }

Where I switch the location of my view directory using a $this->admin_view() within a controller. Maybe you could set up a component that manages such a system in which it falls back on to a 'base' location for a view.

edited May '15

All in all though you seem to be looking to acheive multiple shared views, see http://forum.phalconphp.com/discussion/478/multi-module-shared-views-dual-location- for a few examples on how to acheive this with the directory structure and / or the declaration of views.

Just to follow up on this again I have managed to achieve somewhat the initial idea of what I was after:

https://github.com/phalcon/cphalcon/issues/10513#issuecomment-113649171

Thanks for the input!