How do I grab the page number in the controller of my php code and also set it?
The current page number will be in the Controller params.
Try debugging it to view its contents, or using the DebugKit:
// Within your controller action:
debug($this->request->params);
Following gives you the page no in cakephp 2
$this->request->params['named']['page']
Related
Good Morning. I have a question how to pass multiple params from one controller to another contorller without using location.path and also the id values won't show on url.when i using service setter and getter , id values were gone when refreshed the page.can you please some idea how to stop showing the param values on url and pass to next controller in Angular js 1.x
I am refreshing a page in my controller with return $this->redirect($this->request->here);
Which works great.
But I want to add a GET variable to the refresh. Does anyone know how to do this?
It is not possible to use the Cakephp redirect function with controller and action because of the routing I am using. So this is not going to work for me
return $this->redirect($this->request->here . '?key=val');
I followed Andrew Perkins excellent tutorial on setting up permissions in CakePHP 2.0.
My question, however, relates to how to use the allow and deny method in the Pages controller. Currently I have $this->Auth->allow('display') which allows all methods in the Pages controller to be view.
What if I only want the home page allowed but the rest denied? How do I code that?
Thanks in advance.
Make sure you have copied the PageController.php to your app/Controller folder. Then, add a beforeFilter callback method and set access based on the passed page parameter:
public function beforeFilter() {
// Use $this->request->pass to get the requested page name/id
// Decide on access with $this->Auth->allow()
}
This should solve your problem.
You can find more information on request's lifecycle in CakePHP manual. That's pretty useful stuff.
Have you tried this code?
You can out it into your PageController or into your Controller directly
$views = array ('index'); //array of view that you want allow
$this->Auth->allow($views);
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?
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.
I have a standard cakePHP backend but I'm not using the cake pagination helper. My existing frontend provides pagination params in the form "startIndex, numberOfResults" vs. "page". It would be great if from within the controller action I could just parse my startIndex, numberOfResults params, calc the proper page and then do something like:
paginate['page'] = $pageNumber;
before the paginate() call. No such luck. So my question is, how can I set the paginator page from within the controller? As a bonus: Where is cake parsing the page named param? Where does it store the page value used for the paginate call?
Since It's a 1.2 application. You should try changing $this->params['url']['page'] like this:
$this->params['url']['page'] = $pageNumber;
Source: CakePHP 1.2