【发布时间】:2019-03-09 10:13:01
【问题描述】:
我已经创建了一个自定义 php mvc 模式,我正在尝试加载在我的项目中使用 composer 安装的 mailgun,但我收到了无法打开流:没有此类文件或目录错误,我无法解决它
这是我的页面结构
公开/index.php
<?php
require_once('../app/bootstrap.php');
// Init Core Library
$init = new Core;
app/libraries/core.php 这是创建 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) 中创建了一个配置类,它包含 mailgun api 密钥和域名,将在位于库文件夹中的邮件类中调用。作曲家。 json 文件位于库文件夹中
【问题讨论】:
-
您的自动加载器不正确 - 您应该检查文件在
require_once之前是否确实存在。 -
我已经尝试过
function my_autoloader($class) { if(file_exists('libraries/'.$class.'.php')){ require 'libraries/' .$class.'.php'; } } spl_autoload_register('my_autoloader');,但我得到了 致命错误:未捕获的错误:在 D:\xampp\htdocs\vlake\public\index.php:5 中找不到类“核心”堆栈跟踪:#0 {main} 在第 5 行的 D:\xampp\htdocs\vlake\public\index.php 中抛出 -
请以文本形式分享所有错误信息
标签: php model-view-controller composer-php mailgun