0

i am new comer for cakephp framework. i can not call functions of controller. Controller-

class PagesController extends AppController {
    public $name = 'Pages';
    public $uses = array();

    public function display() {
        $path = func_get_args();

        $count = count($path);
        if (!$count) {
            $this->redirect('/');
        }
        $page = $subpage = $title_for_layout = null;

        if (!empty($path[0])) {
            $page = $path[0];
        }
        if (!empty($path[1])) {
            $subpage = $path[1];
        }
        if (!empty($path[$count - 1])) {
            $title_for_layout = Inflector::humanize($path[$count - 1]);
        }
        $this->set(compact('page', 'subpage', 'title_for_layout'));
        $this->render(implode('/', $path));
    }

    public function register() {
        $this->set('fdf', 'chandan');
        $this->render('home1');
    }
}

But i am calling display(). but i am not calling register(). my routes.php file like-

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

Please help me. how to call controller function from view in cakephp. and what setting have to done for it ?.

6
  • maybe you should read up on some basics of MVC design patterns Commented May 15, 2013 at 16:50
  • The view doesnt call controller functions. The dispatcher calls the controller which renders the view Commented May 15, 2013 at 16:52
  • i have worked on codeigniter. where we can write like for call <a href="print site_url('controller_name/fun_name');". Commented May 15, 2013 at 16:52
  • What controller are you working in? You need to explain your code better Commented May 15, 2013 at 16:53
  • PagesController is working Commented May 15, 2013 at 16:57

1 Answer 1

1

A few points I would make, the routes file is for defining custom slugs/url, take a look at your first route definition here:

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

This is saying that "www.mysite.com/" should link to the controller pages, the action display and pass the first parameter as home.

This can be accessed by doing "www.mysite.com/pages/display/home" in short - but using "/" as a route is tidier. The general rule is "www.mysite.com/controller/action/param1/param2/etc.."

So following this logic you would access your new action method like such: "www.mysite.com/pages/register"

That being said... When using MVC you should really follow the conventions set out, if you're going to create a register method you should really contain it within a controller which deals with user accounts i.e. "UsersController" - "www.mysite.com/users/register"

Also, you shouldn't really need to use $this->render() unless you have to render a separate view under special conditions.

To sum up, contain all actions within a relevant controller (i.e. www.mysite.com/users/login and www.mysite.com/users/register), never directly specify $this->render unless you really need to render something other than the default (/users/register.ctp would be the default for www.mysite.com/users/register) and routes are used to create tidier or custom urls.

I would highly recommend you read and follow the blog tutorial to grasp these concepts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.