Hans
Hans

Reputation: 568

CakePHP how to pass a parameter to controller?

In CakePHP I am trying to pass a simple parameter to my controller class method. However it looks like the parameter has to be visible in the URL. Can't I pass a parameter without it being visible in the URL?

My routing:

Router::connect(
    '/',
    array(
        'controller' => 'Pages',
        'action' => 'display'
    ),
    array(
        'pass' => array(
            'pageName' =>'home'
        )
    )
);

And my Controller method:

public function display($p_sPageName=null) {

Upvotes: 1

Views: 3843

Answers (1)

floriank
floriank

Reputation: 25698

Router::connect(
    '/',
    array(
        'controller' => 'Pages',
        'action' => 'display',
        'home',
    ),
);

This should be a default route in a baked application and already present. The book has also a very good section explaining the routing.

Also follow the CakePHP coding standard, this variable name $p_sPageName is bad. Nobody ever knows what $p_s means. This is a very good read about writing clean and readable code.

/**
 * Displays a static page
 *
 * @param string $pageName
 * @return void
 */
    public function display($pageName = null) { /*...*/ }

The doc block should tell you by "@param string $pageName" that it is a string not the variable name. Without documentation this becomes unreadable for everyone who does not know the naming conventions.

Upvotes: 5

Related Questions