Catalyst::View::Template::PHP - Use PHP as a Catalyst templating system
Version 0.02
# create a PHP view in your class with the helper $ myapp_create.pl view PHP Template::PHP # in myapp.conf <Template::PHP> include_path ... ... ... </Template::PHP> # lib/MyApp/View/PHP.pm package MyApp::View::PHP; extends 'Catalyst::View::Template::PHP'; __PACKAGE__->config->{...some php option...} = ... option value ...; sub preprocess { my ($self,$c,$params) = @_; # adjust params as needed to set global PHP variables return $params; } sub postprocess { my ($self,$c,$output) = @_; # post process the PHP output return $output; } # and somewhere else, maybe a controller $c->stash->{template} = '/path/to/some/script.php'; $c->forward( 'MyApp::View::PHP' );
This is a Catalyst view class for the PHP module (a Perl module for embedding PHP into Perl programs). It is similar to and inspired by the Catalyst::View::PHP view class, but that class is written for the PHP::Interpreter module.
The PHP file to be interpreted is specified in $c->stash->{template}. The view class helps set up the appropriate $_SERVER, $_ENV, $_COOKIE, $_FILES, and $_GET/$_POST/$_REQUEST variables in the PHP interpreter. In addition, the contents of $c->stash are exported as global variables (as in Template Toolkit), and the current context will be available in the global variable $c.
$c->stash->{template}
$_SERVER
$_ENV
$_COOKIE
$_FILES
$_GET/$_POST/$_REQUEST
$c->stash
$c
The workflow for this class looks like:
<process
This is the server environment (the environment you're running Catalyst in)
$_GET
$_POST
$_REQUEST
PHP observes slightly different rules for parsing form input into an associative array of parameters than Perl does, so setting these are not quite as simple as copying the relevant Catalyst request components over to PHP.
As with Catalyst::View::TT, the contents of the stash ($c->stash) will be accessible in PHP as global variables.
$HTTP_RAW_POST_DATA
php://input
On POST requests where the content-type is not multipart/form-data or application/x-www-url-form-encoded, the global PHP variable $HTTP_RAW_POST_DATA and the PHP input stream php://input will be set to contain the content of the request.
multipart/form-data
application/x-www-url-form-encoded
Catalyst::View::Template::PHP cannot currently set up PHP's php://input stream, so $HTTP_RAW_POST_DATA is the only way to access the raw request content.
Catalyst::View::Template::PHP
header()
Location: ...
process
Most of these methods are hooks into the "process" method that allow subclasses to intercept and customize the interaction between Perl and PHP.
View constructor
Sets up the PHP interpreter instance, intializes some global variables, and invokes the interpeter on the script named in $c->stash->{template}. Output from the PHP interpreter is put into $c->response->{output}.
$c->response->{output}
Callback just before global variables are initialized in the new PHP interpreter instance. Subclasses may override this method and make adjustments to $params, a hash reference containing the set of global variables that will be set in PHP.
$params
Callback after the PHP interpreter has finished processing the template script. Subclasses may override this method to perform further processing on the output, before the output is passed to $c->response->output. This method MUST RETURN THE OUTPUT, or else all the output will be erased.
$c->response->output
The postprocess method is also a good place to manipulate the headers produced by PHP (see "header_callback"), if such manipulation is desired.
postprocess
Handles a run-time warning message from the PHP interpreter. By default, this message logs a warning to Catalyst with the text "PHP warning:" prepended to the message. A subclass may override this method and do anything they wish with the warning messages.
"PHP warning:"
If the PHP interpreter encounters a run-time or compile-time error, this method will be called with the error message. The default behavior is to log a warning message, but this method can be overridden in the subclass.
Invoked whenever the PHP interpreter calls the PHP header(STRING) function. The default behavior of this callback is to accumulate the list of headers produced by PHP into the list reference $self->{_headers}. Subclasses may override this behavior and handle the headers from PHP anyway they like.
header(STRING)
$self->{_headers}
Called if PHP produced a Location: ... header. Subclasses should override this method and return a false value if they do not want to redirect to the new location.
The default behavior of this method is to copy the PHP response headers to the Catalyst response headers, and set the PHP output to the Catalyst response output. Subclasses may override this method to do something else.
a whitespace-separated list of paths where the PHP interpreter can look for scripts that were referenced in calls to the PHP include, require, include_once, or require_once functions.
include
require
include_once
require_once
If non-zero, sends verbose information about the inputs and outputs of this view class to the Catalyst log (as $c->log->debug(...) messages).
$c->log->debug(...)
If you need to ask, you probably don't need to use this module.
Some people will not need to ask.
Marty O'Brien, <mob at cpan.org>
<mob at cpan.org>
Please report any bugs or feature requests to bug-catalyst-view-template-php at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Template-PHP. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-catalyst-view-template-php at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Catalyst::View::Template::PHP
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-Template-PHP
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Catalyst-View-Template-PHP
CPAN Ratings
http://cpanratings.perl.org/d/Catalyst-View-Template-PHP
Search CPAN
http://search.cpan.org/dist/Catalyst-View-Template-PHP/
PHP (the programming language), PHP (a Perl module to provide a Perl/PHP interface), PHP::Interpreter (another fine Perl/PHP interface, but one that unfortunately doesn't [as of this release] support php v>=5.2), Catalyst::View::PHP (similar to this Catalyst view but uses PHP::Interpreter instead of PHP).
Copyright 2012 Marty O'Brien.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Catalyst::View::Template::PHP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::View::Template::PHP
CPAN shell
perl -MCPAN -e shell install Catalyst::View::Template::PHP
For more information on module installation, please visit the detailed CPAN module installation guide.