CakePHP View change extension - cakephp

How can I change the extension for CakePHP Views from .ctp to .php
I have seen there is this line in /cake/libs/view.php var $ext = '.ctp'; that sets the extension but how can I do it from my /app/ folder so it doesn't effect Cake core files.
Thanks

You can set the extension in your AppController with
public $ext = '.yourext';

This is is reply to Cameron's comment regarding the issue of using multiple extensions in light of the fact cakephp does not allow you to specify multiple extensions.
I am using Mustache for a single site that uses merb, rails2, rails3 and cakephp for different sections of the site. The cake site "receives" mustache files for common layout elements but these templates have a '.mustache' file extension which my cake site will not recognize. My workaround is basically what dhofstet suggests just framed in the context of your specific usecase. In short, create a wrapper that might look something like this:
<?
$tmp = $this->ext;
$this->ext = '.mustache';
?>
<?= $m->render($this->renderElement('moznav/advanced_header'), array('foo' => $bar)) ?><br />
<? $this->ext = $tmp; ?>
When flow returns to the caller, you keep on using your native file extension.

How can I change the extension for CakePHP Views from .ctp to .php
I have seen there is this line in /cake/libs/view.php var $ext =
'.ctp'; that sets the extension but how can I do it from my /app/
folder so it doesn't effect Cake core files.
example:
you have view posts/add.ctp
now you rename add.ctp into add.php
and then you run .../posts/add the message error show:
Error: The view for PostsController::add() was not found.
to your app can understand extention .php, you add line public $ext = '.php' in PostsController.php
now, you run again ..posts/add => okie, cakephp understand extention .php
Notice: if you use atrribute $ext = '.php' but file view named .ctp, cakephp extention .ctp will use by default

I found this post because I had the same problem. This is not mentioned in the Predominant TwigView plugin documentation on Github. I'm tired of those documentations that explains only half of things and with which we have to guess the second half. This is a big waste of time that slows down projects pointlessly.

Related

CakePHP 3 - Downloading regardless of the host and project name

On any particular View, if I include this link:
<?php echo $this->Html->link('Download', ['controller' => 'Uploads', 'action' => 'download',$upload->id]) ?>
A user can download a file based on the $upload->id provided.
I'm using this as my download function in my Uploads controller:
$upload = $this->Uploads->find('all')->first();
$filePath = '\xampp\htdocs\project\webroot\uploads'; //file path to where the uploaded file is
$this->response->file($filePath . DS . $upload->name,
array('download' => true, 'name' => $upload->name)); //finds the corresponding name attribute in the Uploads table of the database, finds a matching name in the uploads directory and then downloads that file.
$this->set(compact('upload'));
However, I was wondering if there was a way to be able to have the file path be more flexible. In my config.php, I have set two variables called $host and $basepath to refer to the web host (localhost) and the project name (project) respectively. I pass these variables to the AppController, and they can be accessed by any View.
But I can't seem to use these variables in the $filePath variable of the download function, so if I or anyone else were to change hosts or the project name, instead of being able to simply change config.php, I or anyone else would have to go and find this and change it as well (and if I had multiple download functions in multiple controllers, they'd have to go and change each and every one of them).
Take a look at the CakePHP global constants. You can easily access the path to your webroot by using WWW_ROOT.
https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html

How to load Google fonts in CakePHP/Using GoogleFontHelper with CakePHP 2.3.6

How can I load Google fonts in CakePHP? I found one helper which is built for CakePHP 2.0. It's here: http://plugins.cakephp.org/p/1277-CakePHP-Font-Plugin
But when I try to integrate it in my AppController using this:
public $helpers = array('Session','Html', 'Js', 'Form', 'GoogleFont');
, the result is:
Strict (2048): Declaration of GoogleFontHelper::url() should be compatible with Helper::url($url = NULL, $full = false) [APP/View/Helper/GoogleFontHelper.php, line 50]
So is this GoogleFontHelper incompatible with CakePHP 2.3.6.?
Or is there another way of loading Google fonts?
Am i missing something, why do you need a helper? Cant you just do..
echo $this->Html->css('http://fonts.googleapis.com/css?family=Open+Sans:300,400,700');
Or add it directly to your css file using #import like kicaj says.

CakePHP 2 : Implement CakePDF plugin by Ceeram

I'm trying to implement the CakePDF plugin designed by Ceeram.
I'm using CakePHP 2 and I work locally using wamp on windows vista. I followed everything from the readme file but I got stucked at some point.
What I'd firstly like to do is converting an HTML link to a PDF using the WkHtmlToPdf engine. I see many people having issues to make it work so I'm gonna detail all the way through in the following different steps.
STEP 1: CakePdf plugin by Ceeram
I downloaded the plugin at https://github.com/ceeram/CakePdf
I extracted the contained folder into app/Plugin/CakePdf
STEP 2: Bootstrap
I added the following lines - app/Config/bootstrap.php :
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf'
),
'orientation' => 'portrait',
'download' => true
));
STEP 3: Controller
I've created my controller "InvoicesController.php" - app/Controller/InvoicesController.php:
class InvoicesController extends AppController {
public $components = array('RequestHandler');
public function view($id = null) {
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
}
}
STEP 4: View
I've created a pdf folder in my view folder and created view.ctp in app/View/Invoices/pdf/view.ctp.
STEP 5: Layout
I've created a pdf folder in my layout folder and created app/View/Layouts/pdf/default.ctp
That's about it. In my view I couldn't make a PDF file from an URL.
Although I have to mention I'm new in OOP and CakePHP so I would be very grateful if you could show me how to have this done. I'm sure it will help others because there many newbies like me who want to do this but because it's all meant for advanced programmers we cannot figure out how to put the pieces together.
Thank you very much for your understanding and your help!
[THIS POST IS MODIFIED EACH TIME THERE IS A NEW ANSWER WHICH IMPROVES IT]
You need to add RequestHandler component, and browse to localhost/invoices/view/1.pdf
Looks like i had forgotten to mention to add RequestHandler component in the readme.
Also for WkHtmlToPdf you need to tell it where it can find it, and since you are on windows the default location surely wont work for you. You can set the location with Configure::write('CakePdf.binary', 'C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe') after having installed it on windows
You are missing this code in app/config/routers.php
Router::parseExtensions();
Router::setExtensions(array('json', 'xml', 'rss', 'pdf'));
details available on:
http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp/

CakePHP generate and save PDF

I am using CakePHP for an application that does auto generation of vouchers to a PDF file. But they work through user clicks. Now I wish to create it automatically and have the file written to the server hard disk. Then later on, these files will get zipped up and sent to an email of my choice.
For the PDFs I use Html2ps/Html2pdf component found in CakePHP. You can view it here http://bakery.cakephp.org/articles/Casmo/2010/06/26/creating-pdf-files-with-html2ps-html2pdf
One issue I have is the formatting doesn't look right. If I have links that look like this:
http://www.mydomain.com/this-is-my-perma-link
They will render this way in the generated PDF:
http://www.mydomain.com/this- is- my- perma- link
And that would be a broken link. I've tried to use other characters to replace the dash but it doesn't work. I am not sure why.
Another issue is, how can I write the generated PDF file to my server hard disk? Is there an option for me to do that and how do I define the destination. Any examples?
Maybe thanks in advance!
Hi you can to use FPDF library . and you can to save file in the server and then redirect to other function.
check the next code.
///Call to library
<?php
App::uses('AppController', 'Controller');
require_once APP . 'Vendor' . DS . 'fpdf17' . DS . 'fpdf.php';
class TestController extends AppController {
public function test(){
--Create fpdf.
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Output('files/Report.pdf');
$pdf->text($x,$y,'Txt');
return $this->redirect(
array('controller' => 'Test', 'action' => 'test_new')
);
}
}

Including JS in default View in Cake PHP

I can do this in any of my View file in Cake PHP:
<?php echo $this->Html->script('myjs.js', false); ?>
but if I do the same thing in my default view (default.ctp) then the JS files don't load i.e. they don't get echoed. I have tried moving includes above and below <?php echo $scripts_for_layout ?> in the default view but they still don't get printed:
<?php echo $scripts_for_layout ?>
<?php echo $this->Html->script('myjs.js'); ?>
<?php echo $this->Html->script('myjs.js'); ?>
<?php echo $scripts_for_layout ?>
Any ideas?
Thank you :).
with or without an extension - this is not the reason why the script do not get echoed,
as mensch mentioned before it makes no difference because of:
source of html helper cakephp 2
if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
$url .= '.js';
}
the reason is:
you have already inserted this script inside your view,
cake checks if the script is already inserted on the page
options - 'once' - Whether or not the script should be checked for uniqueness. If true scripts will only be included once, use false to allow
the same script to be included more than once per request.
by default the value of once is set to TRUE
remove the script from your view first and then try it with or without '.js'
P.S.: why the petervaz answer has worked for you:
because this check:
if ($options['once'] && isset($this->__includedScripts[$url])) {
return null;
}
made before check for file extension
so isset(__includedScripts['myjs']) == false // because first key was __includedScripts['myjs.js']
and you've got your script included
I have a project with scripts added just before $scripts_for_layout and they are displayed fine. The only difference is that I'm not adding the extension .js to the call, like this:
default.ctp:
<head>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo "Site > $title_for_layout"; ?>
</title>
<?php
echo $this->Html->script('jquery-1.6.4');
echo $this->Html->script('jquery-ui-1.8.16.custom.min');
echo $this->Html->script('mercado');
echo $scripts_for_layout;
?>
</head>
Another thing I'd DEFINITELY recommend is to check that loading your JS in a view is 100% OK with everything.
Do NOT use $this->Html->script if you are unsure about:
--- the dependency of scripts on each other
--- the dependency of some plugins on other scripts
--- how many different layouts and/or views you want to use in the future.
For example, some plugins use jQuery but they require jQuery to be loaded BEFORE the plugin itself.
So if you put jQuery in a view or layout, the plugin's JS loads before jQuery and plugin may not work. If you want to make sure some script loads first, you can try this:
in your app/View/Helper/AppHelper.php:
class AppHelper extends Helper {
public $helpers = array('Html', 'Session');
public function beforeRender($viewFile){
echo $this->Html->script('jquery', array('inline'=>false));
}
}
Therefore you don't have to include jQuery in multiple views and it loads first.
$scripts_for_layout in a layout is a placeholder for all the scripts included in views, so if you include it or don't won't have any effect if you load scripts directly in the default.ctp.
That being said, adding the extension or leaving it out of the $this->Html->script() call shouldn't make a difference. What could be the case is that the HtmlHelper isn't called correctly (it's called by default in Cake 2.0), but you should be receiving error messages. Is debug set to 2 in /app/Config/core.php?

Resources