1
votes

I wish to have a page called "index" with a corresponding url "domain/controller/index" and another page called "admin_index" with a corresponding url "domain/admin/controller/index". The trick is that i want both pages to use the same view to render and the same function for the logic while on of the page's parameters are a flag indicating to the view from which url the view is rendered.

I need it because currently in my "index" page I have table with data. The page also has a smart filter for that page which requires a respectful amount of logic in the controller side. My problem is that currently there is an "Edit" button in each line which I don't want to share to all the users.

Currently I'm using the admin prefix to handle this kind of pages by protecting them by limiting the access from the web-server (Apache in my case).

Any ideas of how to implement this without duplicating the controller function?

1

1 Answers

1
votes

Try this (I've tested it on my CakePHP 2.0.x app, but there's nothing in this code that should be 2.0 specific):

//controller
public function index($admin = false) {
    $this->set(compact('admin'));
}
public function admin_index() {
    $this->index(true); //calls the index function to do all that stuff
    $this->render('index'); //tells it to render the 'index' view
}

When you hit the /index page, all should be as normal. When you hit the admin_index, it runs the logic from the index function, then specifies to use the index view.