How to change page title in CakePHP 2.5? - cakephp

From 2.5 Migration Guide:
$title_for_layout is deprecated. Use $this->fetch('title'); and
$this->assign('title', 'your-page-title'); instead.
They work in Views, but what to do in Controller? Using $this->assign() throws Fatal error.

Use
$this->set('title_for_layout', 'List User');
inside controller.

You have to use
$this->assign('title',$title);
in view files.
In layout, You can also use
$this->fetch('title', $title);
to set the title
You can use $this->set('title_for_layout',$title); but you should not as it will be removed very soon

just set this in your controller's function()
$title = 'Title of your page | Site';
$this->set(compact('title'));
then you can use $title in your views to change the title of your page. :)

Related

CakePHP: Add css to the entire Controller?

There is anyway to add a specific css to the entire controller?
If I want to add to a view I simply add:
<?php echo $this->css('css_name'); ?>
But how to add this css to the entire controller?
Edit:
I know I can add the css to the layout, but that is not the case.
I dont know if its possible to give a controller a css.
What i know and use is to give a controller an othor layout like this:
$this->layout = 'name of the layout';
With this you can give it an other Css..
Use it inside a function or for the whole controller in de beforeFilter()
Put inside you code in default.ctp
Path : app/View/Layouts/default.ctp
echo $this->Html->css('css_name');

cake 2.3.x create menu

I need to help with add menu to my apps.
I use cake 2.3.8. I tried to use MenuHelper from this article http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper.
I add this class to lib>Cake>View as MenuHelper.php
Next I added from AppsController $helpers =>array('Menu');
But I don't know what I should to do next. I tried use
$menu->add('link_list','title','url');
in my view or layout but cake doesn't view $menu variable. What I do wrong?
You're using the CakePHP 1.x syntax in your snippet, hence it doesn't work. In CakePHP 2.x, helpers are exposed as a property in the view and you have to use $this->Menu->add('link_list','title','url');.

render a blank view in cakephp

i need to prevent a view to be rendered in a specified case but i can't understand how to prevent it to render.
I tried
$this->autoRender=false
but nothing happened, probably because i'm using an API engine that manage rendering differently from regular controllers. Anyone know any trick to do this?
Using $this->layout = 'ajax' does not seem to be enough.
But using these both lines works:
$this->layout = 'ajax';
$this->render(false);
While searching for a solution, I found this answer. Now when using CakePHP 2.4.x, you could use the following code in your controller:
$this->layout = false;
This will lead to just the view being rendered, without a layout.
It's an old question. The current cake-version is 3.x and there is a easy way to use a blank layout.
Only add the in the controller:
$this->viewBuilder()->autoLayout(false);
Try to use ajax layout $this->layout = 'ajax' this is the default empty layout, which is used for ajax methods.
public function function_without_layout(){
$this->viewBuilder()->autoLayout(false);
echo "hello Brij";
exit;
}
$this->layout = false; is deprecated in CakePHP version 3.
Use $this->viewBuilder()->autoLayout(false); for CakePHP version 3.
Add this in your controller:
$this->autoRender = false;
This works in my project.
The CakePHP 3 autoLayout(false) method from the other answer will still have the system try to locate a corresponding view/template file for the action you're calling. Since I needed no output at all, this didn't work for me, so I needed to also render an empty template.
Creating a blank .ctp file for every empty action you might need isn't an option really, because you'd normally want to have one and reuse it. CakePHP 2 had a $this->viewPath property which would let you configure the controller to look into the app/View folder, but it's CakePHP 3 alternative still looks into the corresponding controller and prefix folders. There is a not-so-obvious way to force CakePHP3 to look for a template in a root view path.
Create src/Template/my_blank_view.ctp
Add the following to your controller action:
$this->viewBuilder()->layout(false);
$this->viewBuilder()->templatePath('.'); // this
$this->viewBuilder()->template('my_blank_view');
Also, I'm using $this->viewBuilder()->layout(false) instead of autoLayout(false) because the latter kind of implies that there might be another layout set later, where the layout(false) just explicitly sets that there's no layout needed.
without knowing anything about API engine you're using, maybe try to make empty layout with empty content and call it in controller as $this->layout = 'empty_layout'

CakePHP Redirect to External URL

In CakePHP, I want to create a custom URL that points from my site to another site.
Example: example.com/google would redirect to http://www.google.com
I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.
If you want to redirect directly form controller to an external url the we can directly use
$this->redirect('http://www.google.com');
from our controller. It will redirect you to the mentioned address. This works perfectly fine.
You don't want a "redirect", you want to create a hyperlink.
Use Cake's built-in Html helper.
In your controller...
var $helpers = array( 'Html' );
In your view...
echo $this->Html->link( 'Google link!', 'http://www.google.com/' );
A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code
$this->redirect( '/contact/success' );
Using CakePHP HTML helper is your best bet.
echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);
If it's simple enough, you could just use straight HTML.
What you need is something like:
Router::redirect('/posts/*', 'http://google.com', array('status' => 302));
This would redirect /posts/* to http://google.com with a HTTP status of 302.
See http://book.cakephp.org/2.0/en/development/routing.html

How to add javascript code in cakePHP view

I have written the following piece of code in my view page
<script type="text/javascript">
function filldetails()
{
document.getElementById('FirstName').value = "hjshjsh";
}
</script>
echo $this->Form->select('students',$student_name,array('onchange' =>filldetails()));
but i am getting an error message
call to undefined function filldetails()
How do I solve this error?
it should be 'onchange' => 'filldetails()'
Unless your project has a specific reason to avoid JS frameworks, you will avoid a lot of long-term headaches by using jQuery instead of pure Javascript.
Rewrite your view thus:
<?php
$selectDomId = $this->Form->domId('students');
$firstnameDomId = $this->Form->domId('Student.first_name');
$this->Html->scriptBlock("
jQuery(function($){
$('#{$selectDomId}').change(function(event){
$('#{$firstnameDomId}').val( $(this).val() );
});
});
",array('inline'=>false));
?>
<?php echo $this->Form->select('students',$student_name,$this->Form->domId(array(),'students'))?>
<?php echo $this->Form->input('Student.first_name')?>
The jQuery takes care of the onChange event handler, so it doesn't clutter your HTML, by hooking onto your dropdown menu's change event.
The use of Helper::domId means you don't have to worry about how CakePHP's helpers generate their id attributes, which is a net win in reliability and maintainability.

Resources