CakePHP pagination custom url - cakephp-2.0

I am trying to implement pagination with custom url, when i try to paginate to the next page i am getting the following error.
The requested address '/adminuser/userlist/page:2' was not found on this server.
Here adminuser is the name of the admin user and userlist is the method to get all the users under this adminuser.
The url will look like this http://domain.com/adminuser/userlist
Here is my code
In view:
<?php $this->paginator->options(array('url' =>
array('path'=>$this->params['path']))); ?>
<div class="pagination">
<ul>
<li class="prevnext disablelink">
<?php echo str_replace('/users/','',$this->paginator->prev('« Prev'));?>
<li>
<?php echo str_replace('/users/','',$this->paginator->numbers());?>
</li>
<li class="prevnext">
<?php echo str_replace('/users/','',$this->paginator->next('Next »'));?>
</ul>
</div>
routes.php
Router::connect('/:sluguser/:action', array('controller' => 'users', 'action' => 'userlist'),array('pass' => array('sluguser')));

For pagination, using the default 'paramType' => 'named' you will need to extend your route to accept also the page parameter (as well as sort and direction). In your routes.php, you can add following:
// Parse only default parameters used for CakePHP's pagination:
Router::connectNamed(false, array('default' => true));
// the route to your controller action and wildcard (*) for the possible named parameter
Router::connect('/:sluguser/:action/*', array('controller' => 'users', 'action' => 'userlist'),array('pass' => array('sluguser')));
Btw. As I have found out recently named parameters are deprecated and totally removed in CakePHP 3.x, so consider using $this->paginate['paramType'] = 'querystring'; instead. Then it will work without those additional routes above.

Related

CakePHP 3.8 changing urls generated by Paginator numbers to relative

The app has some prefixed routes like so:
Router::scope('/ru', function (RouteBuilder $routes) {
$lang = 2;
$routes->setRouteClass(DashedRoute::class);
$routes->connect('/:slug', ['controller' => 'Pages', 'action' => 'pages', 'lang' => $lang])->setPass(['slug']);
$routes->fallbacks(DashedRoute::class);
});
Page in question: http://localhost:8765/ru/smi-o-nas where there are some pagination links using this method:
<?= $this->Paginator->numbers() ?>
It generates links including matched controller and action:
http://localhost:8765/ru/pages/pages/smi-o-nas?page=2
So is it that routes are not set up correctly, or there is an option to make Paginator generate relative links like this:
http://localhost:8765/ru/smi-o-nas?page=2
Looked at the docs, paginator helper code, but all I came up with is setting Paginator config url, which still does no good.
Using this:
$this->Paginator->setConfig('options.url', ['controller' => 'ru', 'action' => 'smi-o-nas']);
Simply sticks /ro/smi-o-nas/ in the middle for the link:
http://localhost:8765/ru/ru/smi-o-nas/smi-o-nas?page=2
So do I need to set up routes in a different way, or is there an option for relative urls?
Update
I figured out that a relative url would not work for paginator as it will result in urls like "localhost:8765/ru/ru/smi-o-nas/smi-o-nas?page=2?page=3"
Dodgy solution
<?php for($page_number = 1; $page_number <= $this->Paginator->counter("{{pages}}"); $page_number ++):?>
<?php if ($page_number == $this->Paginator->counter("{{page}}")): ?>
<span><?= $page_number ?></span>
<?php else : ?>
<?= $page_number ?>
<?php endif ; ?>
<?php endfor; ?>

How to get CakePHP 2 to keep 'index' in URL when using HTML Helper

In CakePHP 2.x there is a HTML Helper which allows for creating hyperlinks within Views.
If I use this...
<?php
echo $this->Html->link('Navigation',
array('controller' => 'navigations', 'action' => 'foo')
);
?>
... it will generate a URL to /navigations/foo (NavigationsController::foo).
However if I use index as the action
<?php
echo $this->Html->link('Navigation',
array('controller' => 'navigations', 'action' => 'index')
);
?>
The URL becomes /navigations.
I understand about "index" being a default when it comes to webpages. However, I actually need the URL to be /navigations/index, or at the very least have a trailing slash (/navigations/).
Is this possible?
Fix the AJAX URLs instead
With regards to the explanation in your comment, I would argue that the "correct" way of fixing this problem is to use proper root relative URLs for the AJAX calls, as changing the URL structure only cloakes the underlying problem, that is targeting non-unique resources.
Personally I alwas use Router::url() to generate the URLs, either in the specific script files when serving them via PHP, or by writing out configuration in for example the layout, being it globally so that the scripts can access it when needed:
<script>
var config = {
navigationDataUrl: <?php echo json_encode(
Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
) ?>
};
</script>
or by configuring possibly existing JS objects.
<script>
navigation.dataUrl = <?php echo json_encode(
Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
) ?>;
</script>
Connect routes with an explicit index part
That being said, for the sake of completion, it is possible to force the index part to not be dropped, by connecting routes that explicitly define that part, as opposed to using the :action route element, like:
Router::connect('/:controller/index', array('action' => 'index'));
which would match/catch URL arrays like yours before they are being handled by CakePHPs default controller index catchall route, which looks like:
Router::connect('/:controller', array('action' => 'index'), array('defaultRoute' => true));
https://github.com/cakephp/cakephp/blob/2.10.6/lib/Cake/Config/routes.php#L72

how to link to a specific point on a page with cakephp

I'm trying to get a link in cakephp to point to a specific place on another page but what I imagined would work doesn't.
I'm using
<a name="Telstra"></a>
Telstra
Can anyone tell me the correct way?
You need to use the built-in link function of CakePHP. try to use this code.
<?php echo $this->Html->link('NameOfLink', array('controller' => 'ControllerName', 'action' => 'FunctionName/#Telstra')); ?>
See this url from cakephp docs:
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
For creating link only
echo $this->Html->url(array(
"controller" => "posts",
"action" => "search",
"#" => "first"
));
and in your case for creating the link with anchor tag,
echo $this->Html->link('Telestra',array(
"controller" => "sponsors",
"action" => "index",
"#" => "Telstra"
));
If you want to load on a specific part of the page you must use ID on the element of your target rather than Name.
Example:
//Your target element on a page
<a name="Telstra"></a>
//URL that will redirect you to your target element.
Telstra
Or might as well code it the cakePhp style. :)
//URL
<?php echo $this->Html->link('Telstra', array('controller' => 'YourController', 'action' => 'YourFUnction', '#TargetElement'));
//Target Element
<a name="Telstra"></a>
Good Luck!
you need put a route in the folde of configuration /app/config/routes.php:
Router::connect('/index', array('controller' => 'yourController', 'action' => 'index'));
or
Router::connect('/index/*', array('controller' => 'yourController', 'action' => 'index'));
I think the parameter you're meaning to put is 'id' and not 'name' and I think you can just put #idName instead of the whole link.
For example:
<a id="Telstra"></a>
Telstra
For more information you can check here: http://www.w3schools.com/html/html_links.asp

How to create a static page in cakephp?

Currently am creating one auction website using cakephp. It have a menu bar like about us, contact us. I have created only the default page. So i want to create those pages. advice me how to create.
Old thread, but I found it while trying to do the same in 2.x.
Jack's answer is correct, with a small typo. It should be
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Hopefully this helps someone else, as it did me.
Create an about.ctp in the /app/views/pages/ folder.
Then add Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about')); in the /app/config/routes.php file. You should be able to access it at www.yoursite.com/about
Since the new version of cakephp is freshly out, I'm adding this answer to deal with the newer version (3.x).
To link to a static page you still use the PageController but the code slightly changed.
Here the code you would need in the 3.x version
$routes->connect('/about', ['controller' => 'Pages', 'action' => 'display', 'about']);
You can read more about the new routing system here.
I have no affiliation with cakephp. I added this answer since I found this post while searching how to do this in 3.0
Read more here
Method 1: if you want to create content pages like about us, privacy policy which contents can be changed by an admin interface follow these steps
Step1: Change pagesController
class PagesController extends AppController {
function beforeFilter() {
$this->Auth->allow('content');//to allow to be visible for non-logged in users, if you are using login system
parent::beforeFilter();
}
public function content($id = null, $layout = null, $theme=null) {
if ($layout) $this->layout = $layout;//if you are using mulitple layouts and themes and want to change it dynamicaly
if ($theme) $this->theme = $theme;
$this->set('content', $this->Page->find('first', array('conditions' => array('Page.id' => $id))));
$this->Page->id= $id;
$this->set('title_for_layout', $this->Page->field('title'));
}
}
Step 2: add a table content with fields you need like id, title, content, image, theme,layout etc.
Step 3: In View/Pages add content.ctp
<div class="row innerPage">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="row userInfo">
<div class="col-xs-12 col-sm-12">
<h1 class=" text-left border-title"> <?php echo $content['Page']['title'];?> </h1>
<div class="w100 clearfix">
<?php echo $content['Page']['content'];?>
</div>
</div>
</div>
However you can change html according to your need, I prefer bootstrap framework.
Then you can use it as
<?php echo $this->html->link("Terms of Services", array("controller" => "pages", "action" => "content", 5), array("class" => 'themeprimary','target'=>'_blank')) ?>
This will generate a link yoursite/pages/content/5. 5 is the id of row you want to show the details of.
If you want your link like yoursite/terms then you need one more step to go. In routes.php add this line.
Router::connect('/terms', array('controller' => 'pages', 'action' => 'content',5));
Method 2: You simply need to display content without any database
Step1 :Just create a about.ctp under View/Pages and put the content you want to display
Step 2: Change your pagesController. add a method about
public function about($layout = null) {
$this->set('title_for_layout', 'About');
}
Thats it.
You can use the pages controller for this purpose.
Creating views at APP/views/pages/ with names such as about_us.ctp and contact_us.ctp will allow you to access them at the url:
www.site.com/pages/about_us
you can then change how these URIs look with routing.

how to change part of url from cakephp's paginator options?

have custom pagination in my cakephp view. before that i made some custom routing changes.
problem is that links leads to pages like
http://localhost/myapp/foos/view/news/page:2
instead of
http://localhost/myapp/news/page:2
so, part with foos/view/ not have to be part of the link.
tried to change url with several custom options, like
$this->Paginator->options(array('url' => $this->passedArgs));
but no luck, because i always have foos/view/ in url.
can you help me how can i get rid of that foos/view?
thank you very much in advance!
UPDATE: i manage to do "something", but not enough, by adding following lines:
$options = array('url'=> array('controller' => 'news' ) );
$paginator->options($options);
now, my link looks like:
http://localhost/myapp/news/index/page:2
how can i get rid of that "index" in url?
The following line is more about passing various pieces of URL information to the view:
$this->Paginator->options(array('url' => $this->passedArgs));
I think what you want to look into is the helper declaration in your Controller:
var $helpers = (
'SomeHelper',
'AnotherHelper',
'Paginator' => array(
'url' => array('controller'=>'news')
)
);
If you want finer control of a custom route like the one you have then try
'url' => '/news'
I haven't used PaginatorHelper in a while - so I could be egregiously on the wrong track - but I believe that's a good start.
Also, take a look at the Paginator Helper page for where it mentions $options and then take a look at Router::url() as the former page recommends.
I had a case where I am working on a project using CakePHP 2.1 (This thread is tagged as 1.3) with a dynamic admin route to display pages like this:
Router::connect('/admin/main/*', array('controller' => 'adminPages', 'action' => 'display'));
With a query string parameter, that produces a dynamic url like this: http://mydomain.com/adminPages/main/...?page=1
The link route, was incorrect for our needs and found I could alter the url directly by using this:
$this->Paginator->options(array(
'url' => array(
'controller' => 'admin/main/my-display',
)
));
For me it made a link: http://mydomain.com/admin/main/my-display?page=1 - which was the correct url we were looking for. If I used a string, as described above, it appends itself to the url, like: http://mydomain.com/adminPages/main/.../admin/main/my-display?page=1
In view :
<?php
$this->Paginator->options(array('url' => array('controller' => '','action' =>'your-custom-url')));
?>
In routes.php :
<?php
Router::connect('/your-custom-url/*', array('controller' => 'Controller', 'action' => 'function'));
?>

Resources