CakePHP: Add css to the entire Controller? - cakephp

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');

Related

How to send variables to an element in CakePHP 3.x from a controller and keep variables when using element in a different view?

I created an application that allows admins to save slider content to the database and now I want to include the slider on the home page. I have a Slides controller with a slider function that just send the slides content to the slider view.
Here is that controller function:
public function slider()
{
$slides = $this->Slides->find('all');
$this->set('slides', $slides);
$this->set('_serialize', ['slides']);
}
The view for that function only has the following in it:
<?= $this->element('slider'); ?>
I then created an element file called slider and process the slides there. When I go to url /slides/slider the slider is working, but when I go to the root or home page, the slider is empty. It doesn't seem to be keeping the $slides variable in the element.
On the home page:
<?= $this->element('slider'); ?> // then the rest of the home page follows this.
So how do I keep the variable or make it so that I can have the slider view on the home page as well?
The best practice in your case is to use Cells:
View cells are small mini-controllers that can invoke view logic and
render out templates. They provide a light-weight modular replacement
to requestAction().
From the base App dir run cd bin from the console
then run cake bake cell Slider from the console
Go to src/View/Cell/SliderCell.php.
edit the display function,
as the following
public function display()
{
$this->loadModel('Slides');
$slides= $this->Slides->find('all');
$this->set('slides', $slides);
}
Now go to the src/Template/Cell/Slides/display.ctp, and play with $slides in the tepmlate.
To render the Cell anywhere just use this: <?= $this->cell('Slider') ?>
The variables are not sent from the Controller to an element, but from a view.
What you can do is set variables in your controller like this :
$this->set('myvariable','any value');
And, according to cakephp 3 documentation, in the view you can pass the parameter like this:
echo $this->element('helpbox', [
"varToElement" => $myvariable
]);
Source : https://book.cakephp.org/3.0/en/views.html#passing-variables-into-an-element

How to change page title in CakePHP 2.5?

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. :)

Load files that aren't within the Elements or View folders?

Hi I'm trying to make something like CMS with widgets so I have a folder widgets in my app folder there are my widgets for example app/Widgets/UsersOnline/UserOnline.php
I loading them from db with widgets model in beforefilter method in appController and there i pass them to the view, so with a widget helper I would like to render them on the position. But i can't get to the folder widgets/UserOnline/view.ctp with view->element() method this method require file to be in Elements/.
TLDR / Actual Question:
Is there any way to load files in view outside the view/ and /elements ? Thanks in advance.
You can use relative paths when calling the element:
<?php echo $this->Element('../../Widgets/UsersOnline/UserOnline'); ?>
Don't forget to name your element file 'UserOnline.ctp' too.

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'

How do I add an script to all actions of a controller?

I have an script file called 'game.js' which I want to add to all actions inside game_controller.php.
I think there is a better solution than copy and paste "Html->script('game', array('inline' => false)); ?>" inside all my ctp files. How do I do that?
I suggest that you create a sperate layout for given pages and put the script in the layout. If you simply MUST do this, then you can use the beforeRender() method like so:
cakephp: can I set $scripts_for_layout from within a controller?

Resources