cakephp get realpath of view and layout - cakephp

How can i retrieve absolute path of view and layout file in cakephp action.
I cant find useful method or property in
// in action
var_dump($this);
Thank
desire out put is
/home/sweb/www/cakeapp/app/View/Layout/mylayout.ctp
/home/sweb/www/cakeapp/app/View/Mycntl/myaction.ctp

I would use this to create a full path.
APP . 'View' . DS . 'Layout' . DS . $this->layout
Which will output something like this for you,
string '/Users/david/Sites/ExampleSite/app/View/Layout/default' (length=56)
In order to get the view, I'd use
APP . 'View' . DS . $this->modelClass . DS . $this->view

Related

How to import/use static class with namespace

I have been at this for a couple days now. Seems like the simplest thing in the world but for the life of me I cannot get this to work.
I am trying to use this class in my Controller:
https://github.com/neitanod/forceutf8
The class is named Encoding.php and it has a namespace ForceUTF8.
I've tried all of the following:
App::uses('Encoding','Vendor'); //(copying Encoding.php directly in the Vendor dir)
App::uses('Encoding','Vendor/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
App::uses('Encoding’,’Lib'); //(copying Encoding.php directly in the Lib dir)
App::uses('Encoding’,’Lib/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
require_once(APP . 'Vendor' . DS . 'Encoding.php'); //(use plain old php require_once to load class from Vendor dir)
No matter what I do I get the same error: Class 'Encoding' not found.
I find the CakePHP documentation to be very confusing on this subject. On the one hand it says not to use App::uses for non-CakePHP classes because the classes might not follow CakePHP standards. Fair enough. So instead they say to use App::import. But then there are tons of posts saying that App::import is nothing more than a wrapper for require_once.
So after not being able to get App::uses or App::import to work, I tried require_once. Same error. Then I found a post here on so saying even when using require_once, you still have to 'initialize' the class in order to CakePHP to be able to see/use it. And how is that done? App::uses. So I'm back where I started.
Something tells me the namespace is causing the problem. When I use require_once the class is found (I'm pretty sure) because, for example, if I change
require_once(APP . 'Vendor' . DS . 'Encoding.php');
to
require_once(APP . 'Vendor' . DS . 'blabla.php');
it gives me an error, file not found.
But even though require_once seems to find it, CakePHP still says class not found.
How can I load a namespaced vendor file?
In Cakephp 2.x, follow following steps
at top of class file add
use \Dropbox as dbx;
[Dropbox is vendor directory in my case]
use \Dropbox as dbx;
App::build(array('Vendor/Dropbox' => array('%s' . 'Dropbox' . DS)), App::REGISTER);
// Autoload the classes in the 'vendors'
spl_autoload_register(function ($class) {
foreach (App::path('Vendor') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
Now call your class method.
Cake2.x does not support namespaces so you need write your own loader
spl_autoload_register(function ($class) {
foreach (App::path('Vendor') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
more info here:
http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/
Base on this article:
http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/comment-page-1
I'm using the paypal sdk. And this is my code.
In bootstrap.php:
// Add 'vendors' as sub-package of 'Vendor' to the application
App::build(array('Vendor/vendors' => array('%s' . 'vendors' . DS)), App::REGISTER);
// Autoload the classes in the 'vendors'
spl_autoload_register(function ($class) {
foreach (App::path('Vendor/vendors') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
In the PaypalComponent:
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
Use those classes in function:
$this->apiContext = new ApiContext(
new OAuthTokenCredential()
);

Using CakePdf data not showing

I am in a problem with CakePDF. I am using CakePHP 2.3.6. I setup everything alright, bootstrap & core files are ok.
Now, I want to generate many pdf files and download them. Downloading is ok, but when I download the files, I see that the generated pdf files say that Undefined $data, where I send $data to the view file.
Here is my Controller code:
App::uses('CakePdf', 'CakePdf.pdf');
// ...
public function createPdf($id) {
$data = $this->DBTable->find('all', array(
'conditions' => array(
'DBTable.id' => $id
)
));
$CakePdf = new CakePdf();
$path = APP . DS . 'pdf' . DS;
foreach($data as $key => $var) {
$CakePdf->template('create', 'default');
$this->set('var', $var);
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => $var['id'] . ".pdf"
);
$CakePdf->write($path . $var['id'] . ".pdf");
}
}
Here, I am downloading the files some other way, not related to this problem, because downloading is ok. When I run this, all files are downloaded, and when I see the files, they say "Udefined var", looks like they didn't receive and variable called "$var".
I put the view file & the layout exactly where ceeram said.
What should I do now ? Please help me.
At last I got a solution to this. In the Controller function :
$data=$this->ModelName->find('all');
$CakePdf=new CakePdf();
$CakePdf->template('view_file','default');
$this->pdfConfig=array('orientation'=>'portrait','filename'=>$data['name']);
$CakePdf->viewVars(array('data'=>$data));
$CakePdf->write($path.$data['name'].".pdf");
Here, we have to store view_file.ctp in View/Pdf/, and default in View/Layouts/pdf/, where view_file.ctp is the view file which we want to convert in pdf, and default is the layout for this specific view file.
Here, viewVars() function passes the data to the view file, just like :
$this->set->('data',$data);
Let me know if anything is not clear here.
Thanks.

Output URLS to json

I want to generate web URLs to files uploaded by users, and serialize them.
In the controller I can do something like:
$myURL = Router::url('/', TRUE) . 'files' . DS . $relationName . DS . 'attachment' . DS . $attachmentData['dir'] . $attachmentData['attachment'];
But if the filename has spaces, it will not be escaped.
I know I can use the html helper in the view, but this means I will not be able to use the serialize magic from the controller.Also I don't want to break cake by using the HTML helper in the controller, or model.
I's there a way to create web URLs (http://example.c.../someimage.jpg) to files in webroot from the controller?
I tried the following tests variables passed in serialize:
$test = 'http://my.url.com/test.jpg';
$test1 = 'http://my.url.com/test file.jpg';
$test2 = urlencode('http://my.url.com/test file.jpg');
And the result is:
"test": "http:\/\/my.url.com\/test.jpg",
"test1": "http:\/\/my.url.com\/test file.jpg",
"test2": "http%3A%2F%2Fmy.url.com%2Ftest+file.jpg"
So I don't see any problem, urls are properly JSON encoded in test and test1 and test2 is already urlencoded from PHP so it is passed as is.

Autoload a namespaced (PSR-0) directory of classes in CakePHP?

I'm trying to import Assetic (https://github.com/kriswallsmith/assetic) classes.
Managed to do the ugly :
App::import('Vendor', 'LessphpFilter', array('file' => 'assetic' . DS . 'src' . DS . 'Assetic' . DS . 'Filter' . DS . 'LessphpFilter.php'));
But it crashes on a sub-required file.
Any idea how to do achieve this in a clean way?
I had similar issues a couple of weeks ago and haven't found a really clean/satisfying way to do this. But I managed to solve problems with sub-required files by adding paths to the include path before the import. Like so:
$pathExtra = APP.'Vendor'.DS.PATH_SEPARATOR.APP.'Vendor'.DS.'pear'.DS;
$path = ini_get('include_path');
$path = $pathExtra . PATH_SEPARATOR . $path;
ini_set('include_path', $path);
App::import('Vendor', 'consumer', array('file' => 'Auth'.DS.'OpenID'.DS.'Consumer.php'));

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

Resources