Action Method

Navigation:  Development > MVC (Model View Controller) > Controller >

Action Method

Previous pageReturn to chapter overviewNext page

Action method is a Controller Class Method creates Web Page.
 
An action method has below criteria

Naming Conversion

URL format

URL Parameter

Attach View

Set Layout

Pass Value in View

Load Add-on

Set Page Parameter

 

Naming Conversion

Action method always contain a keyword "Action" at the end of the name.
 
A function in controller is accessible from browser only if Action keyword.is added

 

For example:

public function searchAction()
{
}

 

URL Format
Generally MVC follow below conversion to browse a page.
 
Format:
[Domain Name]/[Controller Name]/[Action Method name]
 
Example: www.example.com/home/search
 

If we skip Action Method in URL then indexAction is called by default.

 

For example, if we browse www.example.com/page then by default indexAction() method invoke in Page controller.
 

Note: This URL formatting is customizable by URI Manager to make it user friendly.

 

URL Parameter
We can pass parameter from URL that pass as action method's function argument.
 
For example if we have a URL like www.example.com/home/search/advanced
 
Then the value "advanced' will assign $srcType variable in below method:
 
public function searchAction($srcType=null)
{
}

 

In same process we can pass multiple value like "/value1/value2/value3 .../valueN"  which will save in searchAction($arg1,$arg2,$arg3....$argN).
 

Attach View
An action method always look for a template in view folder to attached in output. We keep all view in below path
 
/development/view/[Theme Name]/[Controller Name]/[Action Method Name]

 

For example: /development/view/rainboo/home/search.phtml
 
See also View

 

Set Layout

Layout is a View that define basic presentation of a web page. We can define the layout from action method by using below variable
 
Example:
$this->layout = 'default';

 
See list of layout
 

Parameter

Description

default

Set default theme

admin

Set Admin Panel Theme

blank

Load Action Template only

empty

Unload all templates

 

Pass Value in View

Following methods used to assign values in View
 
Set Method

$this->set([Variable Name],[Value]);
 
For example:
// Assign a variable 
$this->set('Action',$action);
 
//Assign String
$this->set('section_title','Search');
 
// Assign Array
$this->set(
   'row',
    array(
       "id"=>"5",
       "title"=>"Hello WOrld"
    )
);
 

We receive the value in variable that we assign in set method. In the above example we have used.  $action, $section_title, $row

 

Magic Method
We can use magic method to assign value.
 
$this->setVariableName(Value); 
 
Example: 
// Assign Value in Action Method 
$this->setFirstName('Jhon Ragan');
 
// Receive Value in template 
$this->getFirstName();