I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.
Related
I have just started exploring laravel but I have one confusion . I know how to create own custom function file and make it available globally using compose.json file but I was trying to figure out how laravel's helper function like route() , view() are accessible without including there source file and I can't find any auto discovery in composer.json file neither in any Service Provider .
PS : I have only checked in in Providers/ Directory.
Can anyone tell me how this thing works?
Through composer Laravel has defined which files should be autoloaded. With the line in the composer.json file in Laravel/framework it specifies what should be autoloaded.
It loads the following file.
You can create similar autoloaders if you prefer, but having to much logic in such helpers could easily become an anti pattern. As the logic, is a little more hidden than class based logic, when people have to look through your projet.
On your composer.json file on the root directory of your laravel app, look for the entry autoload.
This means all methods on those directories are autoloaded.
That is why if you have (newly) created a method / function within those directory and it doesn't work (or not found) as expected, you need to run composer dump-autoload to make sure that everything has been loaded.
That's also where I put my custom helper file:
"files": [
"app/Helpers/helpers.php"
]
All function here will then be available on all controllers, traits and views.
i want to use Requests for PHP in my yii project.
I installed the yii2 basic template, and copied the Requests Class File (and the Requests directory) to root/vendor.
I include the requests.php file with this line
include('../vendor/Requests.php');
But i always get an error that yii cant find the requests.php class.
What i need to do to implement the requests class?
Use Composer, as proposed in project README. Run this command in root of your project:
composer require rmccue/requests
I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.
I have a local project with loaded with Composer libs. I uploaded this project to my FTP and received errors, connected with not found classes from some libs.
Can I simply copy vendor/ folder to FTP or I missed something?
Error received:
Fatal error: Class 'AAA\Core\Tools' not found in /home/aaa/public_html/api.php on line 11
api.php:
<?php
use AAA\Core\Tools;
require_once("./vendor/autoload.php");
require_once("./api/" . Tools::getFieldValue('controller') . ".php");
All works fine on localhost!
Linux has a case sensitive file system. That means that files Alex.txt and alex.txt are the same thing in Windows, but not in Linux. Actually on Linux both can happily live in the same directory:
$ tree .
.
├── alex.txt
└── Alex.txt
0 directories, 2 files
Taking this into consideration, I would try to double check that the paths you are using in your namespace are actually identical to what is found at the file system level. (i.e: AAA directory with only uppercase letters ; Core directory capitalized and Tools.php file capitalized)
If you want to keep your existing file system layout, you can use PSR-4 to explicitly tell Composer how to map the namespace to the file system:
Change autoload section from your composer.json:
{
"autoload": {
"psr-4": {"AAA\\DB\\": "db/"}
}
}
where db/ is the actual path from the file system
Update the autoloader:
$ composer dump-autoload
This will take care of directory names, but doesn't apply for files. Meaning that any file inside db/ must be named exactly as used in namespace (for a usage as use AAA\DB\DBCore the file must be db/DBCore.php).
If your file is named dbcore.php and you want to reference it in your namespace as DBCore, you can use classmap feature from Composer:
"autoload": {
"classmap": [
"db/dbcore.php"
]
}
dbcore.php:
<?php
namespace AAA\DB;
class DBCore
{
}
And you can use it as usual:
<?php
require_once("./vendor/autoload.php");
$dbCore = new \AAA\DB\DBCore();
Firstly I would check the autoloader files composer has generated to make sure the paths are valid on your linux server.
Another simple but common issue is that on windows the folder and file names are not case sensitive however they are on Linux. Double check that the folders and files have the correct case as if not it won't find them to auto load.
Rather than trying to upload via FTP which I think is going to be tricky if not impossible to get right, I would suggest you explore getting composer working on your hosting environment.
Composer is entirely PHP based, so should run anywhere that PHP is running.
If you don't have command line access, you can use something like PHPShell which gives you a PHP based command line on which you can then run Composer.
See this other SO answer to get some tips on how to use PHPShell.
Another option is to build a little PHP wrapper that you actually run by visiting it in your browser, in the classic PHP way. See this other SO answer for some tips on how to do that.
Bottom line, you should really look at getting Composer running on your server rather than trying to bodge it another way.
Once you have done your composer process on the server, you must remove the PHPShell or composer wrapper you created so that you don't leave any security holes.
Did you tell the composer where your Class 'AAA\Core\Tools' is?
You can even add your own code to the autoloader by adding an autoload field > to composer.json.
{
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}
Composer is not meant to be used that way (i.e. you shouldn't be manually transferring vendor directories from one environment to another).
As you add dependencies to your project, the composer.json file will contain those dependencies. When you run composer install or update on your localhost, it "locks" the current version of those dependencies for your project and stores them in the composer.lock file. You should be transferring the composer.json and composer.lock files from your development environment to your production environment and then running composer install on your production environment as part of your deployment process. When you run composer install on your production environment, Composer will look at your composer.lock file and install the specified versions of the dependencies in the vendor directory of the production environment.
You may want to review the composer documentation regarding basic usage (https://getcomposer.org/doc/01-basic-usage.md) and command line options (https://getcomposer.org/doc/03-cli.md) for more details.
The Yii2 preview was recently released and is available on github. I want to take it for a test drive, but the "documentation" so far gets outdated almost instantly since it is still under heavy development. I have tried to follow this guide on creating a simple CRUD app with Yii2, but it fails at the step:
php yiic.php app/create /var/www/yii2
With the error:
Could not open input file: yiic.php
Indicating that there is no file called yiic.php. The only folder within the framework folder is yii (framework/yii), and within that folder there is no file yiic.php, only Yii.php which when called in the command line gives the command list:
The following commands are available:
- asset
- cache
- help
- message
- migrate
Anyone managed to successfully setup a Yii2 app? Care to share how you got it done?
Seems like yiic has been removed for now, there are alternatives though, so read on.
It's all in the early stages, so the following method could break in the coming days/weeks/months. Therefore use with caution.
There are 2 ways to do this now:
Use composer. (I recommend this option.)
Directly copy the contents of yii2/apps/ directory to your apps directory, depending on the type of app you want to try.
There are currently 2 options for type of app - advanced, and basic. Both are in their respective directories within the yii2/apps/ directory, i.e yii2/apps/advanced and yii2/apps/basic.
For basic go through the basic readme.md, and for advanced go through the advanced readme.md.
The directions for using composer are given in the respective readme.md files. Including them here for completeness:
Basic app:
Install composer, if you don't have it.
Use composer to install the app alongwith dependencies(Yii):
php path/to/composer.phar create-project --stability=dev yiisoft/yii2-app-basic my_yii2_trial
Access app from http://localhost/my_yii2_trial/www
Advanced app:
Install composer, if you don't have it.
Use composer to install the app alongwith dependencies(Yii):
php path/to/composer.phar create-project --stability=dev yiisoft/yii2-app-advanced my_yii2_trial
According to readme, after step 2 app should be accessible, but composer was failing(see issue 439). With schmunk's tip, ran the install or install.bat command that gets copied by composer: ./install . Selected development environment (by entering choice 0 in the instructions that show up when running install command). Update: The command has been renamed to init, composer doesn't fail anymore, with fix from Qiang (check the issue 439 for more details).
Access app at: http://localhost/my_yii2_trial/frontend/www or http://localhost/my_yii2_trial/backstage/www
Here's how to copy the directory and get it working:
Basic app:
create your web-accessible directory for the app : my_yii2_trial
copy all files and folders from yii2/apps/basic/ directory to my_yii2_trial/ directory.
modify the my_yii2_trial/www/index.php file to point to the correct path for Yii.php.
For me it was within yii2/framework/yii/
comment the line that tries to include ../vendor/autoload.php file, I couldn't find that file anywhere, so its probably for some future use. it is the autoloader provided by composer.
Access from browser : http://localhost/my_yii2_trial/www
Advanced app:
create your web-accessible directory for the app : my_yii2_trial
copy all files and folders from yii2/apps/advanced/ directory to my_yii2_trial/ directory.
modify the my_yii2_trial/frontend/www/index.php file to point to the correct path for Yii.php. Similarly modify backstage/www/index.php.
comment the line that tries to include ../vendor/autoload.php file in both the index.php of backstage and frontend.
Access app at: http://localhost/my_yii2_trial/frontend/www or http://localhost/my_yii2_trial/backstage/www
Some important links to read more about this: issue 77, issue 108, issue 131, and wiki comment.
I am not sure how composer's autoloader is being used, so can't comment on that. Also in future versions, backstage might be renamed to backend.