How can I set the paginator page from within the controller in cakePHP - cakephp

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

Related

How to use one Route for a controller to hit all actions in Laravel 8

I switched to Laravel 8 from CakePhp and I would like to know if it's possible to use only one route for all the actions in a controller. I found "Implicit Controllers" for Laravel 4.2 but it's not working on version 8.
Is not possible to use one route for all actions. However what you can do is use the resource function so that laravel will automatically generate the routes using the CRUD model. You can find the documentation in: https://laravel.com/docs/9.x/controllers#resource-controllers. So, you could declare the route as:
Route::resource('photos', PhotoController::class);
And that will generate:
Route::get('/posts', 'PostController#index');
Route::get('/posts', 'PostController#create');
Route::post('/posts', 'PostController#store');
Route::get('/posts/{postId}', 'PostController#show');
Route::get('/posts/{postId}/edit', 'PostController#edit');
Route::put('/posts/{postId}', 'PostController#update');
Route::delete('/posts/{postId}', 'PostController#destroy');
You can use just this Route:
Route::resource('crud','App\Http\Controllers\CrudsController');
for these function :
index/create/store/show/edit/update/destroy

AngularJS - Changing the URL used in $resource.query method

I'm using AngularJS to integrate with a REST service which has already been built.
The REST API uses the following form for queries:
http://the.site/person/search/smith%20male (this searches for people called smith who are male)
I'm aware that this form isn't the best and will be ultimately getting the API changed to use a URL parameter.
Currently I'm just defining a resource inside my controller:
$scope.Summary = $resource("http://the.site/person/search");
this.Summary.query({ terms : 'smith male' });
but that generates URL's of the form /person/Search?terms=smith%20male
Is there a way to modify or override the URL used? I'm more familiar with Backbone where I was able to provide a url() function in my which generated the correct form of URL.
$scope.Summary = $resource("http://the.site/person/search/:terms");
this.Summary.query({ terms : 'smith male' });

Grabbing the page number in the controller in CakePHP

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']

Cakephp2 pagination with GET parameters

I have a list page with a filter form and Im submitting the form using get method. How to pass the querystring parameters along with the pagination links. I have checked this link
CakePHP pagination and the get parameters but this->passedArgs is coming as empty. Im using cakephp2. Whats the best option to solve this ?
This is some code I use in a CakePHP 1.3 project. I believe it should still work on CakePHP 2.0 as well (put this in the view where your filter form is):
// Make sure we pass any set filters to the Paginator helper
$urlParams = $this->params['url'];
unset($urlParams['url']);
$this->Paginator->options(array('url' => array('?' => http_build_query($urlParams))));

CakePHP - Render a view that is actually plugin's view from Component

Morning guys,
So this is my first time developing a plugin for CakePHP. Here's what I am doing in startUp of the component.
//component
function startUp(&$controller){
//....
if($render){
$controller->render("return", "ajax");
}
}
By default render will look at app/views/<controllers>/return.ctp and app/views/layouts/ajax for this render call.
Is there anyway that I can give a directive to render from app/my_plugin/views/awesome_stuffs/return.ctp and app/my_plugin/views/layout/ajax.ctp instead?
I believe the third param of Controller::render($file, $layout, $file) could do the job, but is there any better Cake way of doing things?
Plus, is that considered a good practice to take over controller's rendering function like that?
One way is to call the PLUGIN controller/action URL in your AJAX call, instead of the main app controller/action URL.
ex:
instead of:
http://domain.com/controller/action
you call:
http://domain.com/my_plugin/controller/action
When you do it this way, the plugin views & layouts are called automagically. See:
http://book.cakephp.org/view/1118/Plugin-Tips
http://book.cakephp.org/view/1115/Plugin-Views
Otherwise, the only way I know of is manually setting paths as you mentioned or controller-wide via:
var $viewPath = 'path/to/plugin/views/';
var $layoutPath = 'path/to/plugin/layouts/';
You might want to try setting $this->view to the plugin dotted view file you want to render.
add to your source
$controller->plugin = "pluginname";

Resources