在Codeigniter 4中添加Facebook库 [英] Adding Facebook Library in Codeigniter 4

查看:56
本文介绍了在Codeigniter 4中添加Facebook库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目使用CI 4,我需要Facebook登录才能获取在fb-JS-sdk中完成的用户数据,然后将数据保存到DB并为该用户启动会话,因此看起来像是直接通过Facebook登录名登录,但这不是指向这里,

I am Using CI 4 for my project and I need Facebook Login to get user data which I have done in fb-JS-sdk and after that I save data to DB and start session for that user so It looks like it's directly loged in by facebook login, but that's not point here,

现在,使用Facebook登录后,用户将从支付网关捐款,并将其重定向到成功页面,该页面将显示感谢您的支付消息,现在我想在成功页面上添加更多内容=我想在用户的FB墙上张贴消息我出于正当理由刚刚在现场捐款...等等...".

Now After Login with Facebook user will donate from payment gatway and it will be redirected to success page which will show thank you for payment message, now I want to add more on success page = I want to post on user's FB wall with message that "I have just donated on site for foo reason...blah...".

我知道我需要Fb用户令牌,这是从数据库中保存的js-sdk登录fb时已经拥有的令牌.

I know I need Fb user token for that which is I have already during fb login from js-sdk saved in DB.

我对Codeigniter还是很陌生,不知道如何添加Facebook库并在我的控制器中使用/初始化它.我没有使用作曲家.

I am very new to codeigniter and don't know How to add facebook library and use/initialize it in my controller. I am not using composer.

因此我将fb-php-sdk添加到了我的应用程序/库(从github下载)

So I added fb-php-sdk into my app/Libraries (downloaded from github)

所以我尝试了:

<?php 
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Libraries\Facebook;

class User extends BaseController {

    public function __construct(){

        helper('url','form');
        
    }

    public function success() {
        require_once ('Libraries/Facebook/autoload.php');
        $fb = new Facebook(); // loads and creates instance
        
        $a = $fb->getDefaultGraphVersion();
        print_r($a);

        $session = session();

        $page = [
            'name' => 'success_payment',
            'title' => $_SESSION['fname']." ".$_SESSION['lname']." Thank You - ".SITENAME,
            'fb_token' => $_SESSION['fb_token'],
        ];

        echo view('templates/header', $page);
        echo view('success_payment'); //this template have success msg for successful payment..thank u and all
        echo view('templates/footer');

    }

}

我遇到错误.

Warning: Uncaught ErrorException: require_once(Libraries/Facebook/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\bb\app\Controllers\User.php:55 Stack trace: #0 C:\xampp\htdocs\bb\app\Controllers\User.php(55): CodeIgniter\Debug\Exceptions->errorHandler(2, 'require_once(Li...', 'C:\\xampp\\htdocs...', 55, Array) #1 C:\xampp\htdocs\bb\app\Controllers\User.php(55): require_once() #2 C:\xampp\htdocs\bb\system\CodeIgniter.php(918): App\Controllers\User->success() #3 C:\xampp\htdocs\bb\system\CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\User)) #4 C:\xampp\htdocs\bb\system\CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false) #5 C:\xampp\htdocs\bb\public\index.php(45): CodeIgniter\CodeIgniter->run() #6 {main} thrown in C:\xampp\htdocs\bb\app\Controllers\User.php on line 55

Fatal error: App\Controllers\User::success(): Failed opening required 'Libraries/Facebook/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\bb\app\Controllers\User.php on line 55

有人可以指导我如何在CI4中集成自定义库吗??很抱歉没有使用作曲家以及英语不好.

can Anybody Guide me how to intgrate custom library in CI4.?? Sorry for not using composer as well as poor english.

推荐答案

最简单的库加载方式,特别是在完全支持composer的Codeigniter 4中,正在使用composer.

Easiest way to load the library, specially in Codeigniter 4 that fully supports composer is using composer.

在您的项目中,做一个简单的事情:

In your project just do a simple:

$ composer require facebook/graph-sdk

之后,您的库将自动加载,因此您只需声明适当的名称空间即可在控制器上使用它.

After that your library will be auto-loaded so you should just declare the proper namespace to use it on your controllers.

如果您不能真正使用composer,则实际上可以正确地做第一件事,并将facebook库添加到您的App/Libraries文件夹或您的app/third_party foder中.但是,在这种情况下,您需要在自动加载时注册该库,或者只是在控制器上手动加载该库.

In the case you can't really use composer you actually did the first bit right and added the facebook library to you App/Libraries folder or your app/third_party foder. However in this case you need to either register said library on your autoload or just load it manually on your controller.

如果您将其保存在third_party文件夹中,则可以像这样自动加载它:

In case you have it in your third_party folder you can autoload it like so:

$classmap = [
    'Facebook' => APPPATH .'third_party/_insertFilenameHere_.php'
];

如果以上方法均未找到该类,并且该类未命名空间,则自动加载器将在/app/Libraries和/app/Models目录中查找以尝试找到文件.

If neither of the above methods finds the class, and the class is not namespaced, the autoloader will look in the /app/Libraries and /app/Models directories to attempt to locate the files.

如果您选择将库添加到Libraries文件夹中,则也需要将其添加到自动加载中.

If you opt by adding the library to your Libraries folder then you need to add that to your autoload too.

$psr4 = [
    'App'         => APPPATH,                // To ensure filters, etc still found,
    APP_NAMESPACE => APPPATH,                // For custom namespace
    'Config'      => APPPATH . 'Config',
    'Libraries'   => APPPATH . 'Libraries'   // Your custom Libraries
];

这样,您的App/Libraries文件夹中的所有库都将可以使用.

This way all libraries in your App/Libraries folder will be ready to use.

这就是为什么我要去第三方代码而不是库.library文件夹通常用于为应用程序构建的类,而不是完整的第三方代码.那应该在作曲家的程序包或第三方文件夹中.

This is why I would go to the third_party code and not the libraries. The libraries folder is normally used for classes that you built for your application and not full blown third party code. That should be in a composer package or the third party folder.

这篇关于在Codeigniter 4中添加Facebook库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆