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

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?

Related

Vtiger CRM: How I can load modified .tpl file?

I want to load a modified .tpl file vtigercrm\layouts\vlayout\modules\Vtiger\ListViewContents.tpl to a .php file from which I get HTML data after using AJAX and javascript . It's something feasible?
Im assuming that you want to view that template in the DetailView. In that case you can make a widget in module/Vtiger/models/DetailView.php in getWidget(). Just copy paste and change. The link you give here should go to a function in /module/Vtiger/views/Detail.php.
you should add your new function in the constructor.
$this->exposeMethod('Yourfunctionname');
Here you can copy any function and change it. you need to feed your variables to smarty with :
$viewer->assign('SMARTYVARNAME', $phpVar);
to fill the template. And finally you need to referance the template.
echo $viewer->view('tplname.tpl', $moduleName, 'true');
im not able to comment yet so i hope this is enough.

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

how to call two different .ctp file in one function in cakephp?

I have 2 view files index.ctp and indexMobile.ctp.
i have to use one single controller logincontroller.php and a single function for both ctp file.
If you want to render a specific view use $this->render in your controller
$this->render('your_view_file');
see Rendering a specific view
Here is my example:
// Changing the "layout" (app/View/Layouts") if you want.
// Change this if you want a different layout for this view.
$this->layout='pdf';
// Folder where is the .ctp View file.
// This is also optional, if your view is in some other folder.
$this->viewPath = 'Pdf';
// Call the "{$view}.ctp" in "Pdf" folder (app/View/Pdf/{$view}.ctp).
// Use this for choosing the right View.
$this->render($view);

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'

Resources