How to change include directory of Zend Framework - zend-framework

I get the following error messages:
Warning: include_once(Zend\Db.php) [function.include-once]:
failed to open stream: No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: include_once() [function.include]:
Failed opening 'Zend\Db.php' for inclusion (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: require_once(Zend/Exception.php)
[function.require-once]: failed to open stream:
No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
Fatal error: require_once() [function.require]:
Failed opening required 'Zend/Exception.php' (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
i want to include ZendXXX\Db.php
how to change it

create a directory (say 'lib'), and put your Zend directory in it. so your directory structure looks like this:
- application
- lib
|- Zend
- wwwroot
|- index.php
now you should add lib to your include path. edit your index.php file:
$includePath = array();
$includePath[] = '.';
$includePath[] = './../application';
$includePath[] = './../lib';
$includePath[] = get_include_path();
$includePath = implode(PATH_SEPARATOR,$includePath);
set_include_path($includePath);
now you have your lib in your include path. you can include all Zend components like this:
include 'Zend/Loader.php';
require_once 'Zend/Db.php';
the best way is too include Zend_Loader first and then use it to load classes. do this:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Db');
you can also register to autoload classes. just add this line to your code after all those before:
Zend_Loader::registerAutoLoad('Zend_Loader',true);
now you do not need to include files to call classes. just instanciate your classes:
$session = new Zend_Session_Namespace('user');
there is no need to include 'Zend/Session/Namespace.php'.

Use set_include_path(). See PHP.net documentation
Example:
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');

I usually store the framework files under a "library" folder:
application
public_html
library
Zend
Common
etc....
and then in my bootstrap file, or front controller, I add that "library" folder to the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . '../library');
See also:
Choosing Your Application's Directory Layout.
Create the Filesystem Layout.

The reason the other suggestions say anything about doing that, is because it's a bad move - in other words, you're doing it wrong.
You can create a subdirectory and name it Zendxxx, but then you have to add that to your include_path, and change it, whenever you put a newly named version up.
I'd hazard a guess, and say that you don't have a good way to test the website (so you want to lock it to a particular version of ZF), and further, that you aren't using revision control, so you want all the previous versions of code in the site-directory to be able to go back to, if you find a problem when you change the live-running code directly on the server.

project without library And including library from one location
project C:\xampp\htdocs\my\application
library C:\xampp\Zend\library
make changes in index.php
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR,
array(realpath(APPLICATION_PATH.'/../../../Zend/library'),get_include_path(),)));

Related

How to run custom php script from doc root in magento2

I wanted to add custom php script to magento2 root folder and run from the browser.I tried to add it in magento2 root folder but it redirects to 404 page.
I also tried to add it in pub folder but no success.
Also cleared cache and empty generation folder.
I am using nginx server
If you are using nginx configuration that comes with magento you need to put a file inside pub folder to allow access to it from the browser as pub is the document root of the vhost. Magento root dir is one level up. Second of all default config for nginx allows only to access index.php, get.php, static.php, report.php, 404.php and 503.php files. Any other are not processed by the php. You can see this in line with location ~ (index|get|static|report|404|503)\.php$ { in nginx.conf.sample. If you are not using it check your config for similar rule. To allow another file to be accessible from browser simple add another name after 503 or change entire brackets with location ~* \.php$ {
Source: https://magento.stackexchange.com/a/97290/1883
For example you can get product name in custom script by this step
step 1: create index.php at root of magento 2
magento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
step 2: create customScript.php
magento2/test/customScript.php
<?php
class customScript
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 12;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
Now you can run this custom script by
http://10.16.16.196/magento2/test/
As stated by #Ranjit, the /pub folder must be your Magento root folder.
The correct way to run a standalone php script on Magento would be:
On nginx:
Locate location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { and add your file there.
I.e:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|myphp)\.php$ {
Then you can access yourstore.com/myphp.php.
On Apache:
Simply add the file under /pub folder. I.e.: /pub/myphp.php.
Apache rewrites rule will redirect to index.php if the file or folder doesn't exist.
In my case on Apache (cPanel) the problem was that file permissions of .php files should not be writable by group or others to be served directly, else Magento's 404 would open.
So in my case to serve file directly I had to set file permission to -rw-r--r-- (on Linux).
This is the mostly issue with servers using suPHP.
Putting this here in case someone runs into same situation...

PHP Built-in Webserver and Relative Paths

TL;DR
Does PHP 5.4 built-in webserver have any bug or restriction about relative paths? Or does it need to be properly (and additionally) configured?
When I used to programming actively I had a system working under URI routing using these lines in a .htaccess file:
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [L]
The FrontController received the Request, find the proper route from given URI in a SQLITE database and the Dispatcher call the the Action Controller.
It worked very nicely with Apache. Today, several months later I decided to run my Test Application with PHP 5.4 built-in webserver.
First thing I noticed, obviously, .htaccess don't work so I used code file instead:
<?php
if( preg_match( '/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"] ) ) {
return false;
}
include __DIR__ . '/index.php';
And started the webserver like this:
php.exe -c "php.ini" -S "localhost:8080" "path\to\testfolder\routing.php"
So far, so good. Everything my application need to bootstrap could be accomplished by modifying the include_path like this:
set_include_path(
'.' . PATH_SEPARATOR . realpath( '../common/next' )
);
Being next the core folder of all modules inside a folder for with everything common to all applications I have. And it doesn't need any further explanation for this purpose.
None of the AutoLoader techniques I've ever saw was able to autoload themselves, so the only class manually required is my Autoloader. But after running the Test Application I received an error because my AutoLoader could not be found. o.O
I always was very suspicious about realpath() so I decided to change it with the full and absolute path of this next directory and it worked. It shouldn't be needed to do as I did, but it worked.
My autoloader was loaded and successfully registered by spl_autoload_register(). For the reference, this is the autoloading function (only the Closure, of course):
function( $classname ) {
$classname = stream_resolve_include_path(
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php'
);
if( $classname !== FALSE ) {
include $classname;
}
};
However, resources located whithin index.php path, like the MVC classes, could not be found. So I did something else I also should not be doing and added the working directory to the include_path. And again, manually, without rely on realpath():
set_include_path(
'.' . PATH_SEPARATOR . 'path/to/common/next'
. PATH_SEPARATOR . 'path/to/htdocs/testfolder/'
);
And it worked again... Almost! >.<
The most of Applications I can create with this system works quite well with my Standard Router, based on SQLITE databases. And to make things even easier this Router looks for a predefined SQLITE file within the working directory.
Of course, I also provide a way to change this default entry just in case and because of this I check if this file exist and trigger an error if it doesn't.
And this is the specific error I'm seeing. The checking routine is like this:
if( ! file_exists( $this -> options -> dbPath ) ) {
throw RouterException::connectionFailure(
'Routes Database File %s doesn\'t exist in Data Directory',
array( $this -> options -> dbPath )
);
}
The dbPath entry, if not changed, uses a constant value Data/Routes.sqlite, relatively to working directory.
If, again, again, I set the absolute path manually, everything (really) works, the the Request flow reached the Action Controllers successfully.
What's going on?
This a bug in PHP's built-in web server that is still not fixed, as of PHP version 5.6.30.
In short, the web server does not redirect to www.foo.com/bar/ if www.foo./bar was requested and happens to be a directory. The client being server www.foo.com/bar, assumes it is a file (because of the missing slash at the end), so all subsequent relative links will be fetched relative to www.foo.com/instead of www.foo.com/bar/.
A bug ticket was opened back in 2013 but was mistakenly set to a status of "Not a Bug".
I'm experiencing a similar issue in 2017, so I left a comment on the bug ticket.
Edit : Just noticed that #jens-a-koch opened the ticket I linked to. I was not awar of his comment on the original question.

How to configure and use Mailgun's SDK with composer-php?

I installed Composer and a SDK for Mailgun's service. These are the steps i followed:
# current directory
cd ~
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Add Mailgun as a dependency
php composer.phar require mailgun/mailgun-php:~1.7
According to the instructions, all I did after that was (index.php):
<?php
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# First, instantiate the SDK with your API credentials and define your domain.
$mg = new Mailgun("key-my-key-goes-here-987654321");
$domain = "somedomain.com";
Then, I tried to get the list of bounced emails:
$data = $mg->get("$domain/bounces", array('limit' => 15, 'skip' => 0));
var_dump($data);
...and I'm getting this error:
Warning: require(vendor/autoload.php): failed to open stream: No such
file or directory in /var/www/html/index.php on line 2 Fatal error:
require(): Failed opening required 'vendor/autoload.php'
(include_path='.:/usr/share/pear:/usr/share/php') in
/var/www/html/index.php on line 2
So I'm guessing it has something to do with composer's installation/configuration perhaps? Thanks for any help...
The way you programmed it, you must have the following files all in the same directory:
composer.json
index.php (your test script)
And you must have run the composer require command while being inside this directory. This will also create a directory named vendor here, and add plenty of files, amongst them vendor/autoload.php.
If however your test script isn't in this location, the require call will not find the file where you tell PHP to find it. This isn't any failure of Composer, but simply the fact that you have to include that file according to your situation, not by copy&paste code. If you change the path of your test script, you have to change the path of the vendor directory as well.

Including JPGRAPH library into a Zend framework project

When trying to include the JPGRAPH library in a Zend Studio project, I got the error
Fatal error: Class 'Graph' not found in C:\Program Files\Zend\Apache2\htdocs\NewStokV4\application\controllers\StatsController.php on line 49
when executing my code.
I tried to follow, unsuccessfully, some tutorials on the net, but none were complete nor clear to me. (I'm new in Zend framework development.)
this is what i get when trying include... or require...
Warning: require_once(vendors/jpgraph-3.5.0b1/src/jpgraph.php) [function.require-once]: failed to open stream: No such file or directory in C:\Program Files\Zend\Apache2\htdocs\NewStokV4\application\controllers\StatsController.php on line 15
Fatal error: require_once() [function.require]: Failed opening required 'vendors/jpgraph-3.5.0b1/src/jpgraph.php' (include_path='C:\Program Files\Zend \Apache2\htdocs\NewStokV4\vendors\Oft_Framework-G1R1C0/vendors/minify-2.1.5/min/lib;C:\Program Files\Zend\Apache2\htdocs\NewStokV4\vendors\Oft_Framework-G1R1C0/vendors/htmlpurifier-4.4.0/library;C:\Program Files\Zend\Apache2\htdocs\NewStokV4\vendors\ZendFramework-1.10.7\library;C:\Program Files\Zend\Apache2\htdocs\NewStokV4\vendors\Oft_Framework-G1R1C0\library;C:\Program Files\Zend\Apache2\htdocs\NewStokV4/library;C:\Program Files\Zend\Apache2\htdocs\NewStokV4\vendors\ZendFramework-1.10.7\extras\library') in C:\Program Files\Zend\Apache2\htdocs\NewStokV4\application\controllers\StatsController.php on line 15
Regarding your comment:
Solution A (a bit dirty)
Put the whole JPGRAPH library inside \library\jpgraph folder an include it inside your controller via:
require_once(APPLICATION_PATH . '/../library/jpgraph/jpgraph.php');
Solution B (better)
Check if jpgrah uses namespaces. if yes you might want try to load it with the build in autoloader function of zend. Put the whole JPGRAPH library inside \library\JPGraph folder.
Usage: just add autoloaderNamespaces[] = "<jpgraph_namespace>" to your application.ini where <jpgraph_namespace> is the namespace of jpgraph.
So assuming the namespace is JPGraph:
application.ini:
[...]
includePaths.library = APPLICATION_PATH "/../library"
[...]
autoloaderNamespaces[] = "JPGraph"
in your controller:
[...]
$JPGraph = new JPGraph();
[...]

Blank home page after creating Zend index.phtml

I am just starting with Zend and am having an issue wherein browsing http://localhost renders a blank page. I've set httpd.conf documentroot to /var/www/HTML/public and enabled Zend_layout in bootstrap.PHP. My index.phtml is locatrd in the suggested directory per the .ZF manual.
Does IndexComtroller have to specifically call my index.phtml view? What other things should I check?
Thanks Much!
EDIT: I checked httpd.conf and noted mod rewrite is enabled. .htaccess file in /var/www/html/public has ReWrite Engine ON etc. I've not created a VirtualHost entry in httpd.conf. I simply defined DocumentRoot "/var/www/html/public" Is a virtualhost required to run the framework?
EDIT2: I used php -f /var/www/html/public/index.php and received the following
PHP Warning: require_once(Zend/Application.php): failed to open stream: No such
file or directory in /var/www/html/public/index.php on line 18
PHP Fatal Error: require_once(): Failed opening required 'Zend/Application.php'
(include_path=':.:/usr/share/pear:/usr/share/php') in /var/www/html/public/index.php
on line 18
The first thing to do is make sure you have the appropriate error reporting enabled for development. Make sure these are set in your application.ini file
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Once you've done this, re-test and post back any error messages you receive.
It seems that you did not setup Zend Framework in your include_path - did you copy the Zend-subdirectory of the installation packages libs-folder into /usr/share/php, so that you now have a directory called /usr/share/php/Zend/?