Cakephp breadcrumb - cakephp

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.

Related

Wordpress php use wp_nav_menu to display all pages in menu

I am currently building a wordpress theme. For the menu, I just want to display all the pages that are created in the order that they are created in (i.e. I don't want users to have to go into the 'menu' section of the site and create their own).
I tried using wp_page_menu and this worked HOWEVER I need to add a walker class to expand the functionality, which you can't do with this function. So I need a way to display all my pages with using wp_nav_menu as my code - is there a way to do this?
Here is my code currently:
<nav id="nav">
<?php wp_nav_menu( array( 'walker' => new Clapton_Nav_Walker ) ); ?>
</nav>
Try this
functions.php this below code register a menu for use
<?php register_nav_menu( 'main_menu', 'Primary Menu' ); ?>
// remove <?php ?> tags if it already there
index.php or wherever you want, below code output the menu
<?php wp_nav_menu( array( 'theme_location' => 'main_menu','walker' => new Clapton_Nav_Walker ) ); ?>
Build menu in WordPress Dashboar -> Appearance -> Menus -> Manage Locations

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

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.

Embedding view in another view with CakePHP

I have a news controller with a 'view' action for each news item. In the 'view' action of each news item I would like to include another view, the 'add' action of the comments controller.
Basically, I need a form on each news item's page to add comments.
I have the two views but I can't manage to link them. I tried with elements, but it seems it doesn't render the view for the add comment view.
What should I do?
Thank you very much!
EDIT
Code in element:
<?php echo $this->Form->create('Comment', array('class' => 'big', 'url' => array('controller' => 'comments', 'action' => 'add', $news_id)));?>
<?php
echo $this->Form->input('comment', array('label' => false));
?>
Form->end(__('Submit', true));?>
Code in view:
<?php echo $this->element('add_comment', array('news_id' => $news['News']['id'])); ?>
move the code from your view into an element and then call the element all over the place.

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