How to include files in PHP Phalcon controllers - php

my file structure for phalcon is something like this
--app
-----controllers
-----models
-----views
-----functions
-----libraries
where functions and libraries folder contains some third party scripts like facebook log in etc, when I include the script in my controller classes, I did
include "../functions/facebook.php"
but it gives a file not found error.
How should I include files in the controller classes?

Ideally you should be using an autoloader Phalcon provides for automatically loading classes.
For non-classes, did you try using full path?
include __DIR__ . "/../functions/facebook.php";

Related

Are PHP Namespaces dependent on path of file?

In YII2 Framework
Path of File is root\vendor\yiisoft\yii2-httpclient\Client.php
Namespace defined in above mentioned file is - namespace yii\httpclient;
Now when I use this namespace in other file while setting up Google ReCaptcha
by writing "use yii\httpclient\Client"
then I am getting error "Class yii\httpclient\Client" not found
So I want to know whether namespaces are path dependent ? or is there a routing file or htaccess..etc where I can define the actual path of namespaces used in project, which YII2 compiler will refer to locate the file / class ?
Namespaces themselves are not dependent on file path.
But you are probably mistaking what use clause does.
If you have this line in file:
use yii\httpclient\Client;
It doesn't mean that the class is loaded. It only tells parser that you mean yii\httpclient\Client every time you use Client class in that file.
PHP has something called autoload to make sure you don't have manually require files for each class you are using. Autoloaders are called every time you are using some class if that class hasn't been loaded yet. When they are called they are given the class name and they check if they know how to load that class.
Now, even if the namespaces itself are not dependent on file path autoloaders usually uses those namespaces to decide where to look for the file containing that class.
And as Nigel Ren mentioned in comment, there exist PSR-4 recommendation how to choose namespace and file structure to make sure that autoloader will know where to look for class.
Yii2 projects usually uses 2 autoloaders.
The Yii's own autoloader and autoloader generated by composer.
Since your question is about class that comes from vendor\yiisoft\yii2-httpclient the autoloader generated by composer.
If you check the composer.json file in that package you can see that it has autoloader section with psr-4 key. That tells composer that when it generates its autoloader it should be set to look for any class from yii\httpclient namespace in src folder of that package.
To make sure the composer's autoloader is working properly you have to go through following steps:
The yiisoft\yii2-httpclient package should be installed by composer.
If you need to regenerate composer's autoloader you can run:
composer dump-autoload
The composer autoloader must be included in your application's entry point (usually /web/index.php or /yii files).
Check if those files have this line:
// in case of /web/index.php
require(__DIR__ . '/../vendor/autoload.php');
//in case of /yii
require(__DIR__ . '/vendor/autoload.php');

How to include autoload in module in drupal 7

I want to use aws-sdk for php client in a custom module of drupal. I need to include vendor/autoload.php in module. But when I include it, it gives me error. I have tried to include many ways but did not get success. I added it as:
require __DIR__.'/vendor/autoload.php'; at the top of the file of .module file. Then the website gets crashed. Please could you tell how I should use require __DIR__.'/vendor/autoload.php';
I'm not sure about how drupal handles autoloading of external php modules. But with the experience of working with frameworks like laravel, cakephp and with composer, I'm sure that the target file which is index.php which routes to a number of different controllers in root directory already includes vendor/autoload.php. If it does not include, add the require statement which points to autoload.php relative to index.php or absolute path. Then using namespaces in php, you can use external php modules which are then autoloaded.
require __DIR__.'/vendor/autoload.php'; // Incase vendor directory is in same level as index.php file
require dirname(__DIR__).'/vendor/autoload.php'; // Incase vendor directory is in parent level as index.php file
It's hard to know without the actual error message. If you're getting a white screen you need to enable display of full errors (admin/config/development/logging) so you can see what's actually going on and if the classes you're relying on aren't being included in the first place, or there's some sort of conflict (remember also to clear the cache after changing anything significant).
Here's how I've gone about adding 3rd party packages in custom D7 modules:
generate a composer.json file that installs it in vendor/
require vendor/autoload.php from [modulename].module
In detail:
change to module directory
run composer init
follow prompts (it'll ask "Would you like to define your dependencies interactively" - choose yes then you can search by name)
use drupal-module for type
you can use "proprietary" for license if you don't want anyone else to use your code, this the composer.json validate cleanly (composer validate)
When finished you should have a vendor directory that includes an autoload.php file and /composer directory. "Require" this from your .module file, e.g.:
require 'vendor/autoload.php';
drush cc all
You should now be able to use your code.
Note there's a Drupal module called xautoload - (in my opinion) overkill for this.
I had wondered if a simple files[] = line for the autoload.php in [modulename].info would work - it doesn't, regardless of order.

How to include external PHP classes in Joomla

I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.
But each time I execute the page I receive the error below:
Fatal error: Class 'SomeClassName' not found
is there any other way to include external PHP classes or files in Joomla?
Thanks in advance!
Please use Joomla autoloader. Is better.
<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');
// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
Edit:
Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.
To do the same with require/include you have to be sure to only use if will really use the class.
Source:
http://developer.joomla.org/manual/ch01s04.html
require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once. Something is going wrong there and it has nothing to do with Joomla itself :-)

Trouble setting up php Zend include path

I am trying to set up a PHP path to my Zend Framework. I am very confused on how to do this. My Zend Framework is located at the following location on my server:
amazon/ZendFramework-1.10.3-minimal
I am going to be creating a couple of php files in the amazon/ directory that will require the Zend Framework. My include path is:
include("ZendFramework-1.10.3-minimal/library/Zend/Service/Amazon.php");
However inside of Amazon.php is the line
require_once 'Zend/Rest/Client.php';
...and then Client.php has more dependencies set up like that, and so on.
How can I set up my include path so that Amazon.php and Client.php (and so on) can correctly reference the location of the Zend Framework?
Thanks
You will have to set your include path using set_include_path() in your Bootstrap file if you are using any. (need to check the ZF layout for details if you are using the ZF).
The Loading of the classes will be handled by the Zend Loader when you include the library/Zend/Loader.php file and calling the function that will enable the automatic loading of classes that reside in your library/Zend folder.
When you set the include path to your library, include the library/Zend/Loader.php and call Zend_Loader::registerAutoLoad() I believe will be able to work without problems.
Short example in a file called bootstrap.php
set_include_path('ZendFramework-1.10.3-minimal/library/'.get_include_path());
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

PHP: Storing objects in a Symfony plug-in module's lib/ directory

I am building a Symfony project, and have created a new plug-in named sfUtilsPlugin. I currently have a directory structure that looks like this:
sfUtilsPlugin/
modules/
sfSearchLucene/
actions/
config/
lib/
templates/
Now, in the sfUtilsPlugin/modules/sfSearchLucene/lib directory, I have an object called sfLucene. The idea was that this object is accessible from the Symfony auto loading mechanism, so that it can be instantiated from anywhere within the application.
However, simply adding the sfLucene.class.php file to the sfUtilsPlugin/modules/sfSearchLucene/lib directory does not appear to add it to the autoloader.
Does anyone out there know why this might be happening? Perhaps it is just not possible to automatically use objects stored in this location inside Symfony.
Any advice is appreciated.
Because you are adding this class in lib subdirectory of module sfLucene, it will be autoloaded only if current module is sfLucene.
You have two options:
put this class somewhere into sfUtilsPlugin/lib directory;
require them every time you need it

Categories