How to create a static page in cakephp? - 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.

Related

cakephp two paginations on one controller and one view

I have a controller that has two paginations which is reflected in my view but anytime I want to click on page 2, the controller doesnt know which page it is clicking on and I get an error.
class AsController extends AppController
{
public function view ($id = null)
{
$data1 = $this->paginate('Post','Post.foriegn_id' => $id);
$data1 = $this->paginate('Author','Author,foreign_id' => $id);
// rest of the code goes here
}
}
I have two sections in my view page
<div class="post view"> and div class="author view"
And in each of tehse sections I have the following code:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
Please assist me in any way to rectify this issue
Cheers
It looks like its more of a structure/design issue. You are using the same id to get posts and authors. Do you have a relationship between posts and authors you can just paginate through one of the models? From what I know the pagination built into cake is meant to work with one model per request. Especially due to how the pagination helper uses the url params. Do you have a case where you want to show page 1 of posts and page 2 of authors? Also in your view action you have your second paginate() saving to the same $data1 var which is overwriting the previous. Not sure if this is a typo.
I would suggest only using one main section of pagination per page. If the data of the authors relates to the posts I would suggest containing and displaying it in the same paginated section.

CakePHP pagination custom url

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.

Cakephp breadcrumb

I am a Cakephp beginner
I am creating breadcrumbs from my website, I am not sure what is the difference between using HTML helper and Breadcrumb helper, Html helper seems easier to use, but it seems like I have to add each crumb to each page manually, please correct me if i am wrong.
When I was trying to use the Html helper, I put
<?php echo $this->Html->getCrumbs(' > ', array( 'text' => 'Customers', 'url' => array('controller' => 'customers', 'action' => 'index'), 'escape' => false)); ?>
in my index.ctp
then, i put
<?php $this->Html->addCrumb('Add customer', 'Customers/add'); ?>
in add.ctp
the breadcrumb "Customer" appears on the index.ctp, but when i go to the add.ctp page, no breadcrumb is shown.
I tried putting
echo $this->Html->getCrumbs(' > ', 'Home');
in default.ctp, then the "Add customer" appears after the Home crumb
How can I make it so that, on the add.ctp , the breadcrumb shows like this:
Customers > Add customer
You should take a better look at the documentation in the cookbook, usually it's all there.
Steps are:
Create a place for cakePHP to display the breadcrubs in your layout template, not index.ctp using something like echo $this->Html->getCrumbs(' > ', 'Home');. You should find your default layout in View/Layout/default.ctp.
Add a trail in each page with something like $this->Html->addCrumb('Add customer', 'Customers/add'); for View/Customers/add.ctp
This is all done by the HTML helper, there isn't any different (official) breadcrumb helper.

how to set cakephp paginator url for custom route

I am making blog and url route is like this-
Router::connect('/blog/c/:catid/*',
array('controller' => 'blogarticles', 'action' => 'index'));
it works well with url as- /blog/c/3/other-articles
but when i use paginator in view as
echo $this->Paginator->numbers();
it generates url as- /blogarticles/index/other-articles/page:2
What changes should in make in paginator to generate proper url.
Please suggest possible solution , Thanks in advance
This should solve your problem:
$this->Paginator->options(
array(
'controller' => 'blog',
'action' => 'c',
$catid,
$title
)
);
The trick is to pass blog as the controller and c as the action, and all other variables (NOT LIMITED TO $catid and $title) as additional parameters, sequentially!
NOTE: I assumed here that you have "set" $catid and $title from your Controller, to the current "category id" and "title" respecting. I also assumed that your URLs are always in the format: /blog/c/:catid/:title
You may also want to view my answer to a similar question: https://stackoverflow.com/a/25097693/2862423
What you want is to set the options for the PaginatorHelper for that view:
<?php $this->Paginator->options(array('url' => '/blog/c/3/other-articles')); ?>
CakePHP Book Section on the PaginatorHelper Options Function: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#modifying-the-options-paginatorhelper-uses

What's the best way to include the login/logout links with CakePHP?

I was thinking of putting this in the AppController, but that would mean I would have to have the same code in each view file for this. I want to put this in the header so it would be on every view. I also want there to be some logic behind it where if I am logged in then show only logged out link and my user menu. Similarly, if I am not logged in then show the register and login link. Can anyone give me a hand or point me in the right direction? Thank you!
//layout .ctp
<div class="header">
<?php
if($this->Session->read('Auth')) {
// user is logged in, show logout..user menu etc
echo $this->Html->link('Logout', array('controller' => 'users', 'action' => 'logout'));
} else {
// the user is not logged in
echo $this->Html->link('Login', array('controller' => 'users', 'action' => 'login'));
}
?>
</div>
is one way. If the code is in your layout, it will appear in every view, negating redundant code.
For Cake 3, you will need use this:
$this->request->session()->read('Auth')

Resources