如何在Cakephp 3.9中加载Google SDK

问题描述

我必须将Google插件加载到CakePHP 3.9应用程序中,并且需要在控制器内部使用。

Google插件位于vendor文件夹内 供应商/ Google

任何人都可以为以下几点提供帮助

  • 如何加载插件
  • 如何导入控制器

我已使用以下链接进行参考: https://api.cakephp.org/3.9/class-Cake.Core.Plugin.html

解决方法

我在CakePHP中使用func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! customtv //cell.textLabel?.text = "\(indexPath.row)" // Don't Do this. cell.lbl.text = "\(indexPath.row)" // Do this. return cell } -您无需做任何特殊的事情即可使用它。如果它是通过composer安装的,则已经在Composer的自动加载器中,则可以直接调用它。

例如在运行Google_Client后的composer.json中,我的要求部分列出了Google API:

./composer.phar require google/apiclient:"^2.7"

如果"require": { "google/apiclient": "^2.7",尚未安装./composer.phar install,请确保您运行它。

然后使用该库,我直接将其命名为require,因为它没有命名空间:

\

如果您想知道这是如何工作的-Composer将生成加载public function index() { $client = new \Google_Client(); require期间的类所需的所有信息,并将这些信息粘贴回{{1} },例如install,它应该自动将vendor/composer添加到那里的列表中,例如:

autoload_namespaces.php

按行业标准命名的类(如PSR-4(可能应该是所有现代PHP库!))的类可能位于Google_中,依此类推。它在ClassLoader.php中注册了自己的自动加载器,并在<?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'),中保留了对它的引用-Cake基本上在第1行的第1行调用它 autoloader_psr4.php

vendor/autoload.php

简而言之-只要您通过Composer工作,就不必担心再次自动加载。

此外-如果您能够使用IDE来帮助自动完成类名称空间(如PHPStorm)的填充,那么这可能会使事情变得更容易。

,

对于指定自动加载信息的库,Composer会生成一个vendor / autoload.php文件。您只需添加此文件即可开始使用这些库提供的类,而无需进行任何额外的工作:

require __DIR__ . '/vendor/autoload.php';

更多参考信息:https://getcomposer.org/doc/01-basic-usage.md