I useGoogle_Client
in CakePHP - you shouldn't have to do anything special to use it. If it's installed via composer it's already in Composer's autoloader, you can call it directly.
Ex. Incomposer.json
after running./composer.phar require google/apiclient:"^2.7"
My require section lists the Google API:
"require": {
"google/apiclient": "^2.7",
Make sure you run./composer.phar install
if it wasn't already installed durringrequire
.
Then in to use the library, I just call it directly, prefixed with\
since it's not namespaced:
public function index()
{
$client = new \Google_Client();
If you're curious how this works under the hood - Composer will generate all the information it needs to load classes durringrequire
orinstall
and sticks these in several files back invendor/composer
, such asautoload_namespaces.php
, where it should automatically have addedGoogle_
to the list there, ex:
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
// Lots of class prefixes, but eventually:
'Google_' => array($vendorDir . '/google/apiclient/src'),
Classes that are namespaced to industry standards like PSR-4 (like all modern PHP libs probably should be!) are probably inautoloader_psr4.php
- and so on. It registers it's own autoloader in ClassLoader.php, and sticks a reference to this invendor/autoload.php
- which Cake calls essentially on near line 1 ofwebroot/index.php
:
require dirname(__DIR__) . '/vendor/autoload.php';
So in short - you don't need to worry about autoloading again so long as you're working through Composer.
Also - if you're able to use an IDE which helps with autocompletion of class namespaces, like PHPStorm, that might make things easier.
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and start using the classes that those libraries provide without any extra work:
require __DIR__ . '/vendor/autoload.php';
For more references : https://getcomposer.org/doc/01-basic-usage.md
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CakePHP is a framework that has been around since 2005 and is known for the convenience it gives web developers. It needs very modest settings, does not require the use of XML or YAML files. It has its own ORM, which sets it apart from other similar tools. In terms of security, it is also doing well, in particular, it has a protection system against CSRF attacks.
https://cakephp.org/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.