无法从供应商加载 mailgun class
Cant load mailgun class from vendor
我已经创建了一个自定义 php mvc 模式,我正在尝试加载在我的项目中使用 composer 安装的 mailgun,但是我遇到了无法打开流:没有这样的文件或目录错误,我无法解决
这是我的页面结构
Public/index.php
<?php
require_once('../app/bootstrap.php');
// Init Core Library
$init = new Core;
app/libraries/core.php
这是主要的应用程序核心 class,它创建 url 并加载核心控制器并创建干净的 url 格式
<?php
class Core {
// Set Defaults
protected $currentController = 'Pages'; // Default controller
protected $currentMethod = 'index'; // Default method
protected $params = []; // Set initial empty params array
public function __construct(){
$url = $this->getUrl();
// Look in controllers folder for controller
if(file_exists('../app/controllers/'.ucwords($url[0]).'.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 index
unset($url[0]);
}
// Require the current controller
require_once('../app/controllers/' . $this->currentController . '.php');
// Instantiate the current controller
$this->currentController = new $this->currentController;
// Check if second part of url is set (method)
if(isset($url[1])){
// Check if method/function exists in current controller class
if(method_exists($this->currentController, $url[1])){
// Set current method if it exsists
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// Get params - Any values left over in url are params
$this->params = $url ? array_values($url) : [];
// Call a callback with an array of parameters
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
// Construct URL From $_GET['url']
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
app/libraries/mail.php
<?php
use Mailgun\Mailgun;
class Mail
{
public function send($to, $subject, $text, $html)
{
$client = new GuzzleHttp\Client([
'verify' => false,
]);
$adapter = new Http\Adapter\Guzzle6\Client($client);
$mg = new Mailgun(Config::MAILGUN_API_KEY, $adapter);
$domain = Config::MAILGUN_DOMAIN;
$mg->sendMessage($domain, ['from' => 'test@test.com',
'to' => $to,
'subject' => $subject,
'text' => $text,
'html' => $html]);
}
}
app/bootstrap.php
<?php
// Load Config
require_once 'config/config.php';
// Load Config
require_once 'config.php';
//Load Vendor
require_once 'libraries/vendor/autoload.php';
// Autoload Core Libraries
spl_autoload_register(function($className){
require_once 'libraries/' . $className . '.php';
});
App/controllers/Pages.php
<?php
class Pages extends Controller{
public function __construct(){
$this->shopModel = $this->model('Shop');
}
// Load Homepage
public function index(){
$mail = new Mail();
$send = $mail->send('john_644@hotmail.com','test','this is
test','<h1>this is test</h1>');
$this->view('shops/index' , $data);
}
}
我在应用程序根目录 (App/config.php) 中创建了一个配置 class,它包含 mailgun api 密钥和域名,即将在邮件中调用 class 位于库 folder.The composer.json 文件位于库文件夹
问题出在 bootstrap.php 中的自动加载功能,因为 composer 已经创建了一个自动加载功能,您需要做的就是调用供应商 autoload.php
删除
// Autoload Core Libraries
spl_autoload_register(function($className){
require_once 'libraries/' . $className . '.php';
});
添加这个就可以了
<?php
// Load Config
require_once 'config/config.php';
// Load Config
require_once 'config.php';
//Load Vendor
require_once 'libraries/vendor/autoload.php';
require_once 'libraries/Core.php';
require_once 'libraries/Mail.php';
我已经创建了一个自定义 php mvc 模式,我正在尝试加载在我的项目中使用 composer 安装的 mailgun,但是我遇到了无法打开流:没有这样的文件或目录错误,我无法解决
这是我的页面结构
Public/index.php
<?php
require_once('../app/bootstrap.php');
// Init Core Library
$init = new Core;
app/libraries/core.php 这是主要的应用程序核心 class,它创建 url 并加载核心控制器并创建干净的 url 格式
<?php
class Core {
// Set Defaults
protected $currentController = 'Pages'; // Default controller
protected $currentMethod = 'index'; // Default method
protected $params = []; // Set initial empty params array
public function __construct(){
$url = $this->getUrl();
// Look in controllers folder for controller
if(file_exists('../app/controllers/'.ucwords($url[0]).'.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 index
unset($url[0]);
}
// Require the current controller
require_once('../app/controllers/' . $this->currentController . '.php');
// Instantiate the current controller
$this->currentController = new $this->currentController;
// Check if second part of url is set (method)
if(isset($url[1])){
// Check if method/function exists in current controller class
if(method_exists($this->currentController, $url[1])){
// Set current method if it exsists
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// Get params - Any values left over in url are params
$this->params = $url ? array_values($url) : [];
// Call a callback with an array of parameters
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
// Construct URL From $_GET['url']
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
app/libraries/mail.php
<?php
use Mailgun\Mailgun;
class Mail
{
public function send($to, $subject, $text, $html)
{
$client = new GuzzleHttp\Client([
'verify' => false,
]);
$adapter = new Http\Adapter\Guzzle6\Client($client);
$mg = new Mailgun(Config::MAILGUN_API_KEY, $adapter);
$domain = Config::MAILGUN_DOMAIN;
$mg->sendMessage($domain, ['from' => 'test@test.com',
'to' => $to,
'subject' => $subject,
'text' => $text,
'html' => $html]);
}
}
app/bootstrap.php
<?php
// Load Config
require_once 'config/config.php';
// Load Config
require_once 'config.php';
//Load Vendor
require_once 'libraries/vendor/autoload.php';
// Autoload Core Libraries
spl_autoload_register(function($className){
require_once 'libraries/' . $className . '.php';
});
App/controllers/Pages.php
<?php
class Pages extends Controller{
public function __construct(){
$this->shopModel = $this->model('Shop');
}
// Load Homepage
public function index(){
$mail = new Mail();
$send = $mail->send('john_644@hotmail.com','test','this is
test','<h1>this is test</h1>');
$this->view('shops/index' , $data);
}
}
我在应用程序根目录 (App/config.php) 中创建了一个配置 class,它包含 mailgun api 密钥和域名,即将在邮件中调用 class 位于库 folder.The composer.json 文件位于库文件夹
问题出在 bootstrap.php 中的自动加载功能,因为 composer 已经创建了一个自动加载功能,您需要做的就是调用供应商 autoload.php
删除
// Autoload Core Libraries
spl_autoload_register(function($className){
require_once 'libraries/' . $className . '.php';
});
添加这个就可以了
<?php
// Load Config
require_once 'config/config.php';
// Load Config
require_once 'config.php';
//Load Vendor
require_once 'libraries/vendor/autoload.php';
require_once 'libraries/Core.php';
require_once 'libraries/Mail.php';