Learn how to retrieve the name of the current module within an action or template in Symfony 1.4

In Symfony 1.4, the sfContext provides information about the current application context, such as information about the request, response etc. You will be able to retrieve the name of the module where you're currently running your code from this context. This can be achieved from both the templates (view) and from the actions (controller):

From a template (view)

Within a template, the sfContext instance should be automagically exposed in the view layer as $sf_context so you could simply access the module name with:

// Outputs: "module_name"
echo $sf_context->getModuleName();

From an action (controller)

Alternatively, if you really need to have you can send the module name as a variable accessing the sfContext from the controller (or get the module name in the controller and send some other variable according to the module name): 

<?php

// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->module_name = sfContext::getInstance()->getModuleName();
    }
}

So in this case with the example, in the view indexSuccess.php, we could print the module name with:

// app/frontend/modules/job/templates/indexSuccess.php

// Outputs: "job"
echo $module_name;

Happy coding !


Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person

Sponsors