I've tried doing the following in my 404 error page to create a back link the referring page so that I don't have to rely on javascript.
<?php $this->set('refer', $this->referer()); ?>
<p><?php echo 'Back to previous page'; ?></p>
However it doesn't work... Any ideas why? This code is done in /Errors/error404.ctp
Use the request object :)
$this->request->referer();
In CakePHP3 you can use:
<li><?= $this->Html->link(__('Back'), $this->request->referer()) ?></li>
It should be fine.
Try adding this to the beforeRender method of your app controller:
beforeRender() {
$this->set('refer',$this->referer);
}
It should be available to all views then.
What about using just $_SERVER['HTTP_REFERER'] instead? I know this is not the Cakey way to do it but it seems like it would work in your case.
$this->request->referer() works in .ctp file for Cakephp version 2.8.0
<?php echo $this->Html->link(__d('user', 'Login', true), $this->request->referer(), array('class' => 'login_text')); ?>
Related
I have problem with addCrumb() method it doesn't work for me when I use getCrumbs() everything is fine but with addCrumb() method I don't get anything shown in my view I load HTML helper in my AppView and I did everything by CakePHP book but I still don't get breadcrumb trails this is my code sample:
<?php echo $this->Html->getCrumbs('>', [
'text'=>$this->Html->image('home.png',['alt'=>'Home','height'=>50,'width'=>50]),
'url'=>['controller'=>'Pages','action'=>'display','home'],
'escape'=>false
]);
$this->Html->addCrumb('Users','/users');
$this->Html->addCrumb('Add User',['controller'=>'Users','action'=>'add']); ?>
?>
I also got confused looking at cake documentation at first. But later I figured it out.
The order of your code is wrong. First you have to do
$this->Html->addCrumb('Users','/users');
$this->Html->addCrumb('Add User',['controller'=>'Users','action'=>'add
Then the getCrumbs,
echo $this->Html->getCrumbs('>', [
'text'=>$this->Html->image('home.png',['alt'=>'Home','height'=>50,'width'=>50]),
'url'=>['controller'=>'Pages','action'=>'display','home'],
'escape'=>false
]);
I would like to have the page on user/sidebar on the right in the template design.
Normally, I would include a php file. But I am using Kohana Framework so I have created a view and a controller for this sidebar, and exists on mysite.com/user/sidebar
Now how would i <?php include "/user/sidebar"; ?> correct? I get no such file og dir error for this. I tried full url, but allow_url_include=0
Just looking through the Kohana documentation...
It seems like you can include a "request" inside a view with the following command.
<?php echo Request::factory('user/sidebar')->execute() ?>
See this page for more info: http://kohanaframework.org/3.0/guide/kohana/mvc/views
AndrewR's comment is close. For Kohana 3.2, you'll want to load a view within a view and not a request within a view:
<?php echo View::factory('/user/sidebar'); ?>
or
<?php include Kohana::find_file('views', 'user/sidebar') ?>
Either is acceptable to do.
I want to create the most basic ajax link there is in Cakephp 2.0.
In index.ctp i have
<?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
<div id="success"></div>
in TechnologiesController.php i have
public function view(){
$this->set('msg', 'message');
$this->render('view', 'ajax');
}
and in view.ctp i have
<?php echo $msg;?>
Instead of setting the view in the success div, it navigates to the http://local.cake.com/technologies/view page to display the message.
Any help much appreciated!
By default scripts are cached, and you must explicitly print out the cache. To do this at the end of each page, include this line just before the ending tag:
echo $this->Js->writeBuffer(); // Write cached scripts
I am using this at the end of my default.ctp in the Layouts Folder
Be sure to set $components = array('RequestHandler') in your controller
So, in total the code would look like this - and it works for me (CakePHP 2.2.4):
index.ctp:
<?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
<div id="success"></div>
echo $this->Js->writeBuffer();
Thank you a lot, this helped me understand how things work in 2.0 and above :)
try $this->autoRender = FALSE;
I think you might have to put this:
echo $this->Js->writeBuffer();
somewhere - for example right below the $this->Js->link call.
Hope that helps!
I just put this:
echo $this->Js->writeBuffer();
it work for me, hope i will for you
How do we echo this url link so it displays a link named View instead of the long generated url address? It's this currently I'm using:
echo($value->url."<br>");
The output of the code above would give me the whole url address, so can someone writes how if I want it to be a simple link named View ? Thanks..
P.S The above code currently doesn't display the url in link, I want it to be a click-able link named View.
echo 'View';
I'm surprised that nobody answered with the 'Cake' way to do this. For a straight link, you'll have to tinker a little as the helper expects a path relative to controllers, but it's not difficult. It's the best way to do it if your url is internal to the site, i.e. an action/view handled by CakePHP.
<?php echo $html->link('View', $value->url); ?>
http://book.cakephp.org/view/1442/link
edit:
...and of course a better way to do it in PHP would be to leverage the double quote parsing -
<?php echo "<a href='$url'>View</a>"; ?>
or, if you want to be picky,
<?php echo "View"; ?>
echo 'View';
<?php echo 'View"'; ?>
use some html in it.
this way worked for me:
<td><?php print "<a href='$row[website]'> go </a>"; ?></td>
I'm trying to add a page link in CakePHP using:
<?php echo $html->link('Home', '/notes/index');?>
It doesn't seem to output the proper link.
What I'd want is something like this:
Home
How can I do this?
You'll want to use a url array
<?php echo $html->link('Home', array('controller'=>'notes','action'=>'index')); ?>
This will work for you. You will not be able to use an absolute path :)
Check out the manual page, http://api.cakephp.org/class/html-helper#method-HtmlHelperlink
Create individual controller for this page or create routing for this page in PagesController.