hmz 4dev
hmz 4dev

Reputation: 23

How to call init() method of a controller by default in all actions

class IndexController extend AbstractActionController
{
    construct__()
    {
        $view = new ViewModel();
    }

    public function init()
    {
        $exp = 'exemple';
        $this->view->setVariable('exp', $exp);
    }

    public function indexAction()
    {
        // I Want call init in all action without $this->init();
    }

    public function aboutAction()
    {
        // I Want call init in all action without $this->init();
    }
}

I want call function init() by default on all actions of this controller without manually typing $this->init()

Upvotes: 0

Views: 1201

Answers (2)

hmz 4dev
hmz 4dev

Reputation: 23

// I find solution in my question 
//thanks for all peopole that they help me 

class indexController extends AbstractController{

public function __construct()
{
$this->init();
}

protected function init()
{
$view = new ViewModel();
$view->setVariaa*ble($tt,'message')

return $view;
}

public function indexAction()
{
return $view;
}

Upvotes: 1

gsc
gsc

Reputation: 1559

Let’s assume that your routes are defined like the following (module.config.php):

<?php

namespace Application;

use Zend\Router\Http\Literal;

return [
    // …
    'router' => [
        'routes' => [
            'about' => [
                'options' => [
                    'defaults' => [
                        'action'     => 'about',
                        'controller' => Controller\IndexController::class
                    ],
                    'route' => '/about'
                ],
                'type' => Literal::class
            ],
            'home' => [
                'options' => [
                    'defaults' => [
                        'action'     => 'index',
                        'controller' => Controller\IndexController::class
                    ],
                    'route' => '/'
                ],
                'type' => Literal::class
            ]
        ]
    ]
    // …
];

As you can see, about and home routes point to different actions, while you want them to do the same thing, that is call the IndexController::init() method. But you can simply make both routes point to the same action and, in result, do the same thing in one method instead of calling IndexController::init().

Let’s simplify your IndexController — remove all methods and add a single commonAction:

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function commonAction()
    {
        // perhaps do other things
        return new ViewModel(['exp' => 'exemple']);
    }
}

Now you can go back to module.config.php and instruct the router to use commonAction for both about and home routes. Just replace both

'action' => 'about'

and

'action' => 'index'

with

'action' => 'common'

Upvotes: 0

Related Questions