gpt4 book ai didi

php - 在框架外的 PHP 文件中调用 OpenCart 库函数

转载 作者:可可西里 更新时间:2023-11-01 13:17:39 25 4
gpt4 key购买 nike

在 Controller 中,我可以很容易地调用这样的函数

$this -> user->login('username','password');

但是在单独的 PHP 文件中的框架之外,我如何才能访问该方法?我尝试过类似的方法,但没有成功:

include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php');

$userobj=new User();
$userobj->login('username','password');

你能帮忙吗?

编辑/更新:这是我的 startup.php 文件中的内容:

<?php
// Error Reporting
error_reporting(E_ALL);

// Check Version
if (version_compare(phpversion(), '5.1.0', '<') == true) {
exit('PHP5.1+ Required');
}

// Register Globals
if (ini_get('register_globals')) {
ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');

session_set_cookie_params(0, '/');
session_start();

$globals = array($_REQUEST, $_SESSION, $_SERVER, $_FILES);

foreach ($globals as $global) {
foreach(array_keys($global) as $key) {
unset(${$key});
}
}
}

// Magic Quotes Fix
if (ini_get('magic_quotes_gpc')) {
function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[clean($key)] = clean($value);
}
} else {
$data = stripslashes($data);
}

return $data;
}

$_GET = clean($_GET);
$_POST = clean($_POST);
$_REQUEST = clean($_REQUEST);
$_COOKIE = clean($_COOKIE);
}

if (!ini_get('date.timezone')) {
date_default_timezone_set('UTC');
}

// Windows IIS Compatibility
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
}
}

if (!isset($_SERVER['DOCUMENT_ROOT'])) {
if (isset($_SERVER['PATH_TRANSLATED'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
}
}

if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);

if (isset($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}

if (!isset($_SERVER['HTTP_HOST'])) {
$_SERVER['HTTP_HOST'] = getenv('HTTP_HOST');
}

// Helper
require_once(DIR_SYSTEM . 'helper/json.php');
require_once(DIR_SYSTEM . 'helper/utf8.php');

// Engine
require_once(DIR_SYSTEM . 'engine/action.php');
require_once(DIR_SYSTEM . 'engine/controller.php');
require_once(DIR_SYSTEM . 'engine/front.php');
require_once(DIR_SYSTEM . 'engine/loader.php');
require_once(DIR_SYSTEM . 'engine/model.php');
require_once(DIR_SYSTEM . 'engine/registry.php');

// Common
require_once(DIR_SYSTEM . 'library/cache.php');
require_once(DIR_SYSTEM . 'library/url.php');
require_once(DIR_SYSTEM . 'library/config.php');
require_once(DIR_SYSTEM . 'library/db.php');
require_once(DIR_SYSTEM . 'library/document.php');
require_once(DIR_SYSTEM . 'library/encryption.php');
require_once(DIR_SYSTEM . 'library/image.php');
require_once(DIR_SYSTEM . 'library/language.php');
require_once(DIR_SYSTEM . 'library/log.php');
require_once(DIR_SYSTEM . 'library/mail.php');
require_once(DIR_SYSTEM . 'library/pagination.php');
require_once(DIR_SYSTEM . 'library/request.php');
require_once(DIR_SYSTEM . 'library/response.php');
require_once(DIR_SYSTEM . 'library/session.php');
require_once(DIR_SYSTEM . 'library/template.php');
require_once(DIR_SYSTEM . 'library/openbay.php');
require_once(DIR_SYSTEM . 'library/ebay.php');
require_once(DIR_SYSTEM . 'library/amazon.php');
require_once(DIR_SYSTEM . 'library/amazonus.php');
?>

最佳答案

而不是这样做:

include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php');

(包括 useronly),包含将从 OpenCart 加载所有系统类的 startup.php(可能不需要或者可能与您需要使用这些 OpenCart 类的项目范围发生冲突,但这可以在出现问题时修复)。您还需要包含 OpenCart 的 config.php 以确保 OpenCart 的 DB 类具有连接到已定义 DB 所需的常量。您可能希望通过首先检查文件是否存在来以清晰的方式做到这一点:

$root = $_SERVER['DOCUMENT_ROOT'] . '/mydir/opencart/';

if (file_exists($root . 'config.php')) {
require_once($root . 'config.php');
}
if (file_exists($root . 'system/startup.php')) {
require_once($root . 'system/startup.php');
}
if (file_exists($root . 'system/library/user.php')) {
require_once($root . 'system/library/user.php');
}

然后您可以尝试登录您的用户:

$user = new User();
if ($user->login('username','password')) {
echo 'User was logged in successfully';
} else {
echo 'User not found or username or password do not match.';
}

那将是一个代码示例

拜托,看看 admin/index.php 在创建 User 对象之前如何实例化所有必需的类。

$registry = new Registry();

$loader = new Loader($registry);
$registry->set('load', $loader);

$config = new Config();
$registry->set('config', $config);

$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

// ... do the same for rest of required classes ...

$user = new User($registry);

关于php - 在框架外的 PHP 文件中调用 OpenCart 库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com