How can i use Zend framework in my php project? - php

I want to know how can i use Zend Framework in my php project where my web host doesnt support it. Im using only bunch of the Zend classes, so can I put them inside my include folder and upload it to my site? Will this work?

First include autoloader class.
require_once 'lib/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->registerNamespace('Http\PhpEnvironment', 'lib/Zend/Http');
//Register with spl_autoload:
$loader->register();
and then use below line
$a = new Zend\Http\PhpEnvironment\Request();
echo $a->getQuery()->get();
this is just the demo that you can use loosly coupled zend library to your project.

Here is great place to get started http://framework.zend.com/manual/en/
If you host supports PHP 5, then Zend Framework will work without any problems.
Usually Zend directory is placed inside of lib directory, look at Quick Start.

Related

Using Zend_PDF and Zend Framework, Loading Libraries in PHP?

I’m new to PHP, only having worked with a LAMP stack a little bit. I would like to use Zend_PDF to fill forms using PHP. I am having issues and believe I do not know how to properly load Zend Framework (or external libraries in general). Here is what I have currently:
<?php
include_path='.:/var/www/html/lib/';
require_once'.:/var/www/html/lib/Zend/Loader/ClassMapAutoloader.php';
$pdf = Zend_Pdf::load('.:/var/www/html/forms/test.pdf');
echo count($pdf->pages);
?>
I was using count(); as a test but I’m looking to use the setTextField(); functions. I do not have a “/Loader/Autoloader.php” as referenced in some guides.
How do I properly load the library so that I can use the setTextField(); function?
PHP 5.4.16,
Zend Framework 2.4.4,
CentOS7
There are a few issues with your question.
Firstly, the Zend_Pdf you mentioned above belongs to ZF1, not ZF2. If you really are talking about ZF2, then the class is called ZendPdf and can be used as a standalone component - you do not need to have a full copy of ZF2 available for the autloading (composer will generate an autoloader - you will just need to require that in your script). Last time I checked (which, admittedly, was a couple of years ago), the two versions were functionally equivalent, so you should probably just use the version that matches the version of Zend Framework that you're actually using.
Which brings me to the next issue. Because I wasn't completely sure which version you were referring to, I did a quick text search and discovered that the setTextField() method only exists in Zend_Pdf from ZF1, not the ZendPdf class that is related to ZF2, so I'm not sure why you mentioned ZF2 in your question. But anyway, I figured out how to get the ZF2 version working before I made that discovery, so I've included both methods below for completeness.
Also, you have an error in your require_once statement - it should not have the '.:' included at the start. Now on to my actual answer.
Loading Zend_Pdf from Zend Framework 1 Standalone
This should work:
set_include_path( '/path/to/zf1/library' . PATH_SEPARATOR . get_include_path());
require_once( '/path/to/zf1/library/Zend/Loader/Autoloader.php' );
Zend_Loader_Autoloader::getInstance();
$pdf = new Zend_Pdf();
$pdf->pages[0] = new Zend_Pdf_Page( Zend_Pdf_Page::SIZE_A4 );
$pdf->pages[0]->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 24 );
$pdf->pages[0]->drawText( "Hello world!", 240, 400 );
$pdf->save( 'example.pdf' );
You will obviously need to adjust the paths in the code above in order to make it work properly, but once you do that and run the script, you should end up with example.pdf being created.
Loading ZendPdf from Zend Framework 2 Standalone
Download the ZendPdf project from github (https://github.com/zendframework/ZendPdf) using the instructions on the project page or clone the repository directly using git if you prefer.
Change into the directory where ZendPdf has been placed and run composer install to download the supporting files.
In that same directory (ie. the project root of ZendPdf), create a file called test.php with the following contents:
require 'vendor/autoload.php';
use ZendPdf\PdfDocument;
use ZendPdf\Page;
use ZendPdf\Font;
$pdf = new PdfDocument();
$pdf->pages[0] = new Page( Page::SIZE_A4 );
$pdf->pages[0]->setFont( Font::fontWithName( Font::FONT_HELVETICA ), 24 );
$pdf->pages[0]->drawText( 'Hello world!', 240, 400 );
$pdf->save( 'example.pdf' );
Run php test.php and the example.pdf file should be created.
The magic in that second solution comes from composer, which creates the autoload.php file for you. There are many, many benefits to using this approach, one of which is that you don't have to have a full copy of Zend Framework 2 installed simply to get a working autoloader.

can we use zend library in our own php application

is it compulsory to use zend framework structure for using zend library or we can use zend library in our own php application...
Three steps to using the ZF (assuming ZF1) library in your own app without the entire ZF MVC stack.
Set the include path
Make sure that the Zend library folder is on your php include_path.
Load your class
You could just include each class file before you use it:
require_once 'Zend/Validate/EmailAddress.php';
$validator = new Zend_Validate_EmailAddress();
But it's a pain to do it that way. Typically, it's better to using the autoloader. The easiest way is something like (early in your bootstrap process, perhaps in a common.php file, YMMV):
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Once this is done, you can instantiate/reference on-demand:
$validator = new Zend_Validate_EmailAddress();
Instantiate/Reference
$validator = new Zend_Validate_EmailAddress();
echo $validator->isValid('test#example.com') ? "Cool" : "Fail";

Getting started with Zend PDF and Zend Guard Loader

I'm trying to install Zend PDF in order to fill out editable PDFs on my client's shared hosting account (media temple). I have it enabled now, confirmed in phpinfo http://i.imgur.com/lDiLk.png but after that, I can't find out what I need to get started. If I try loading the Zend_Pdf class, I receive a "Fatal error: Class 'Zend_Loader' not found" message.
These are very different. Zend Guard Loader is used to run PHP scripts encoded by Zend Guard. And Zend Loader component is used, among other things, to simplify the development. For example, this...
$pdf = new Zend_Pdf(); // what's Zend_Pdf, people?
... statement is meaningless to PHP unless it knows what is Zend_Pdf class. Thankfully, in PHP there's a special mechanism of importing these files automatically - autoloading. Here's how to do it with Zend_Loader:
set_include_path(
implode(PATH_SEPARATOR, array(
get_include_path(),
PATH_TO_ZF_LIBRARY
)));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
...
$pdf = new Zend_Pdf(); // no error, as correct file should be imported automatically
Actually, if it's only a single file that will use Zend component, it's not required that you use the autoloading mechanism. A simple require_once (no pun intended) would suffice:
require_once 'Zend\Pdf.php';
...
$pdf = new Zend_Pdf(); // oh, now I know all about Zend_Pdf!
Zend Guard Loader and Zend Loader are not the same thing. Zend Guard is another Zend product and has nothing to do with Zend Framework. Zend Loader is the name of the class loader within Zend Framework.
Make sure that you have a copy of Zend Framework within your application and that you've setup the include path to point at this location.

Using ProcessBuilder in a Silex Project

I am wanting to use the Symfony\Component\Process\ProcessBuilder class and can see that it is included as part of the Silex codebase within the vendors folder. I am using the Silex phar file and assume that because I can readily instantiate other Symfony components like Request, Response and so on that it will correctly locate the file to include when I use the full namespace.
$foo = new Symfony\Component\HttpFoundation\Request(); //works fine
However, when I try and create and instance of it using:
$foo = new Symfony\Component\Process\ProcessBuilder(); //class not found
It gives me a class not found error. Does anyone know why this is and how I can use this class from the Silex phar without including the component seperately within my project?
It looks like the Process Symfony component is not included in the compiled Silex phar file.

How to Use Zend Library without installation of Zend Framework

How to use zend library without using zend framework installation?
I am trying to use zend library(Mail and Mime) without zend framework installation, its not returning any error messages...
but for my project i'm using Mail and Mime library only, How to use Zend Library without installing zend framework ..
Thanks,
Vinoth S
Download Zend Framework and put it into a folder accessible by your PHP. Then either do
include '/path/to/folder/containing/Zend/lib/Zend/Mail.php';
include '/path/to/folder/containing/Zend/lib/Zend/Mime.php';
$mailer = new Zend_Mail;
Or - better and more conventient - setup your autoloader and/or include path so PHP can find the classes directly, without you having to include them.
Also see
the requirements appendix for a detailed list of requirements for Zend Framework.
Register the autoloader and set include path like this:
set_include_path(implode(PATH_SEPARATOR, array(
realpath('./library'),//the path
get_include_path(),
)));
require "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
I've done it more than once to integrate zend libs in other non-zend projects.
Autoloader is not suggested for just inclusion of some libraries as it involves in worse performances (see zend reference about |end_Loader for that).
The best way (from both clear code and performances point of view) is very simple:
1) set the include path: (necessary or you'll have fatal inclusion errors):
set_include_path(implode(PATH_SEPARATOR, array(
'/',
get_include_path(),
)));
2) do a "require_once" of the library/ies you need, following the structure Zend/
e.g:
require_once "Zend/Mail.php";
//you can use now Zend_Mail* classes
note1: you don't have to place a "require_once" of all the needed classes, the main included class already do a require_once of the dependent classes.

Categories