Defyleiti
Defyleiti

Reputation: 555

How to make one action that handles and renders different pages

Is it possible in zf2 that i only have one controller and one action that i can reuse for different pages?

Say.

   Class SiteController extends AbstractActionController{


    public function viewAction(){

       // use for the homepage, product, category etc pages.


    }


    }

Is this possible?

Upvotes: 0

Views: 74

Answers (2)

Purple Hexagon
Purple Hexagon

Reputation: 3578

Could use a parameter to load different content.

Class SiteController extends AbstractActionController{

    public function viewAction(){
       $content = $this->params()->fromRoute('slug');

       // Query or logic using slug to load different content dynamically

    }
}

module.config.php

        'router' => array(
            'routes' => array(
                    'site' => array(
                            'type'    => 'literal',
                            'options' => array(
                                    'route'    => '/site',
                                    'defaults' => array(
                                            'controller' => 'Application\Controller\SiteControllerr',
                                            'action'     => 'index',
                                    ),
                            ),
                            'may_terminate' => true,
                            'child_routes' => array(
                                'view' => array(
                                    'type' => 'segment',
                                    'options' => array(
                                        'route' => '/[:slug]',
                                        'constraints' => array(
                                            'slug' => '[a-zA-Z0-9_-]+'
                                        ),
                                        'defaults' => array(
                                            'action' => 'view'
                                        )
                                    )
                                ),
                            ),
                    ),
            ),
    ),

Now you can have routes like

/site/products /site/categories

Upvotes: 3

Raggamuffin
Raggamuffin

Reputation: 1760

I worked with ZF2 for a bit with PimCore, and I came to the same problem, each controller was specific to a certain view, I figured out by looking at the Zend_View class for a while that I could create a 'loadView' method to work around this:

/**
     * Load and render an arbitrary view.
     * Path is set to the ./website/views/scripts/
     * @param string $name name / path of the view relative to ./website/views/scripts/ (No leading /)
     * @param array $params key => value assoc array of variables to be passed to the view.
     * @return string rendered view string.
     */
    protected function loadView($name, $params=array()) {
        $view = new Zend_View(array('scriptPath' => './website/views/scripts/'));

        if(is_object($params))
            $params = (array)$params;

        foreach($params as $key => $value)
            $view->assign($key, $value);

        return $view->render($name);
    }

That may work for you too.

EDIT** Ofcourse you may need to change the basepath to your view files... :)

Upvotes: 1

Related Questions