cakephp pagination: how to set a different 'page' parameter - cakephp

Is there any way to configure a different param for the current page number?
for example, I have this paginated url:
http://localhost/petproject/posts/index/page:2
And I would like to have the url like this:
http://localhost/petproject/posts/index/my_custom_page_param:2
Thank you!

You could simply use the router to transform the URL into something else that matches your needs by adding a rewrite rule for that URL. Without researching it further I think this is the most easy solution.
Another one might be to check in Controller::beforeFilter() if your custom named arg is set and copy/set it to params['named']['page'].
Or extend the Paginator component and create your customized version of it.

Related

AngularJS change url with $location

I'm trying to change the URL with AngularJS,
but not with a redirect, just change the URL
after an event.
What I need is this:
www.myurl.com/inbox/1 to this www.myurl.com/inbox/25
In other words, just change the last Id.
I'm trying to do this:
$location.path('/inbox/'+id);
But what I'm getting is this:
www.myurl.com/inbox/1#/inbox/25
Angular enforces the idea of one page web app. Browsers don't take any actions on change of anything after '#' value. So the best practice is to add variable attributes in url after '#' value which will keep the base url and attribute look clean on browser's address bar as well as solve your problem. My advice is to keep username, page no. , or any specific attribute id after '#' value.
In your case you should use something like above said
www.myUrl.com/#/inbox/25
or
www.myUrl.com/inbox/#/25
usually angular URLs look like
www.myurl.com/#/inbox/1
in which case
$location.url('/inbox/25');
should work for you
For that you should use .url() instead of .path()
$location.url(`/inbox/${id}`)

AngularJS: How to provide a search facility and cause route change passing in params?

I need to be able to offer a search facility that will show results, I suppose allowing this route to be be bookmarkable.
What are my options here?
Do I just need to create a new route and place a resolve on there so that i can call my rest service and only display the page when its ready. Then i presume I would force a change of route via my existing controller ?
Problem I see is that I need to store a number of items on the URL, all the search params otherwise it won't be bookmarkable. Is the only way to do this by passing ugly querystrings ?
And how would I access the querystrings from angularjs so i could extract my params that i need to send along to the rest service.
I have tried googling for something similar or an example but I can't seem to find anything.
Has anyone done something similar, any pointers would be really helpful.
thanks
The $location service has a search method that acts as both a getter and a setter for querystring parameters.
It will return an object that corresponds to your parameters:
{
foo:123,
bar:'Kittens'
}

AngularJS routing for dynamic urls, how?

I'm trying to understand how can i configure my angularJS routing given the following case:
We have a search page where we display the search results based on tags provided (1..n tags). we would like that a user to be able to parse enter a url as the following and our system to do the search and show the respective results.
The url format should be:
http://mywebsite.com/search/<term1>/<term2>/<termN>...so it could be different number of terms.
I was looking into the route provide and couldn't figure out a way to do it dynamically.
i saw that i could put in the routeprovid:
.when('/search/:searchParams',... but that handles only when i have one term...is there anyway to configure it to take as many terms as is given?
Does this help you at all? Seems to support dynamic routing and you could probably cut apart the :name parameter to do what you wish, perhaps.
http://gregorypratt.github.io/AngularDynamicRouting/
Ken
You could try base64ing your searchParams:
.when('/search/:searchParams', {controller:'SearchCtrl'})
function SearchCtrl($routeParams, $location){
//Assuming your params are an array like ['param1', 'param2', 'param3']
//You could easily adapt this to base64 a JSON object
function encodeParams(params){
return window.btoa(params.join(';'));
}
function decodeParams(string){
return window.atob(string).split(';');
}
var searchParams = decodeParams($routeParams.searchParams);
scope.search = function(params){
$location.path('/search/' + encodeParams(params));
}
}
My solution may be looks not so glad, but it's works at least:
You may organize your routs in way
yoursite.com/term1Name/**:param1**/term2Name/**:param2**/term3Name/**:param3**
To make it's clear, you may do your routes seems like REST routes. For example I'm want to go to a list of a services:
www.yoursite.com/servises/
Go to the one of the services:
www.yoursite.com/servise/:id
And if I'm want to see some of the service details, I'll do:
www.yoursite.com/servise/:id/details
and so
www.yoursite.com/servise/:id/detail/:id

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";

How can I access a parameter sent through the URL within my view files in CakePHP?

I'm new to CakePHP but I've been though their FAQs and guides to no avail. This is so simple that I just must not be thinking straight:
How can I access a parameter sent through the URL within my view files?
Example: http://example.com/view/6
How would I take that parameter ("6") and cycle it through the controller to another view page?
If that's too complex for a quick answer, how can I reference the 6 within the view page itself? The 6 in this situation is the "Id" value in my database, and I need to set it as the "parent" -
Thanks
Parameters can be retrieved like this
$this->params['pass']
Returns an array (numerically indexed) of URL parameters after the Action.
// URL: /posts/view/12/print/narrow
Array
(
[0] => 12
[1] => print
[2] => narrow
)
To access the parameter in your view look in $this->params
The URL, as you have it, will call the 6() method of your ViewController, which is not a valid method name. You may have to play with your routes to make that work.
If you don't want to configure your routes, you'll need the controller in the URL, like so:
http://example.com/thinger/view/6
which will call thingerControllerObject->view("6"). If you want "/view/" to go to a different method, edit the routes. See:
Cake controllers
Cake routes
Use the code below in the view file :
$url=Router::url($this->here, true);
$url_arr=explode("/",$url);
To see the content of $url been exploded simply print it using pr() as below :
pr($url_arr);
It will print associative array, thus you can access any particular number of parameter sent via url.

Resources