iswan
iswan

Reputation: 92

Cakephp Passed arguments

Hello, my code is not working in CakePHP framework and it is showing an error message.

URL:

http://domainname.com/About/param1/param2

Code:

class AboutController extends AppController {

    public $name = 'About';
    public $helpers = array('Html');

    public function index($arg1, $arg2)
    {
        print_r($this->request->params['pass']);
        $this->set('title_for_layout','Sarasavi Bookshop - About Sarasavi Bookshop');
        $this->set('nameforfiles','about');
    }
}

Error message:

Missing Method in AboutController    
Error: The action param1 is not defined in controller AboutController
Error: Create AboutController::param1() in file: app\Controller\AboutController.php.

<?php
class AboutController extends AppController {


    public function param1() {

    }

}

Notice: If you want to customize this error message, create app\View\Errors\missing_action.ctp

After creating the function param1 I can get param2, But I need to get param1 and param2 both, in index function without create another action.

Please help me, Thanks

Upvotes: 2

Views: 2254

Answers (2)

Karisters
Karisters

Reputation: 186

You probably access this URL without specifying action name (index) like this:

/about/param1/param2

This will not work as Cake expects to see action name after controller name. In this case, param1 is treated as action name. You can access your action with URL

/about/index/param1/param2

To overcome this situation, make a new rule to your Router:

Router::connect(
    '/about/:param1/:param2',
    array('controller' => 'about', 'action' => 'index'),
    array('param1', 'param2')
);

Upvotes: 2

Matt Cain
Matt Cain

Reputation: 5768

Your original code would work if you went to http://domainname.com/About/index/param1/param2

If you don't want the index in the url, as I assume you don't, then you need to define a route.

Add this to your routes:

Router::connect(
    '/About/*',
    array('controller' => 'About', 'action' => 'index')
);

to automatically route requests that don't specify an action but do have params to go to your index action. You will need to add a route for any new About actions to stop requests for them from going to index by default though.

Upvotes: 3

Related Questions