【问题标题】:why my main controller is not found to codeIgniter为什么我的主控制器找不到codeIgniter
【发布时间】:2017-09-20 13:36:11
【问题描述】:

您好,我对codeIgniter 中的namespace 有疑问。

我想做的是:

  1. 我已经下载了phpmailercomposer

  2. 它有 2 个 namespaces 他们是

使用 PHPMailer\PHPMailer\PHPMailer;

使用 PHPMailer\PHPMailer\Exception;

当我包含这个namespace 时,它会给出这个error

找不到类“Frontend_Controller”

这是我的控制器代码:

无错误代码 - 此代码工作正常

class Welcome extends Frontend_Controller {
   //my code goes here
}

此代码给出错误

 require "vendor/autoload.php";

 use PHPMailer\PHPMailer\PHPMailer;

 use PHPMailer\PHPMailer\Exception;

class Welcome extends Frontend_Controller {
   // my code goes here....
}

我已经尝试过这个链接,但给出了同样的错误:https://gist.github.com/JeyKeu/7533af3b9b5fd078910d 如果我将代码放入application\config\config.php

请帮助我提前谢谢

【问题讨论】:

  • use 语句应该在类里面...在构造函数里面你必须提到 use 语句...
  • @Mahesh,我试过了,但它给出了相同的error
  • @Mahesh,实际问题出在require这一行` require "vendor/autoload.php"; `
  • 现在你想要什么......你已经使用了 SMTP 邮件功能......对吗?
  • 我的mail 功能与core php 我已经测试过

标签: php codeigniter frameworks


【解决方案1】:

在 config 文件夹中创建一个文件 email.php...

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; 
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'youraccount@gmail.com'; 
    $config['smtp_pass'] = 'test@12#'; 
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n";
?>

在主控制器中...

    $params['mailtype'] = 'html';
    $params['subject'] = ' Something ';
    $this->email->set_mailtype("html");
    $this->email->from('info@test.co.in', 'Application name');
    $this->email->to('example@gmail.com');
    $this->email->subject($params['subject']);
    $this->email->message($this->load->view('your_view_page', $params, true));
    $this->email->send();

【讨论】:

    【解决方案2】:

    CodeIgniter 2.x

    在 index.php 中添加以下行

    require "vendor/autoload.php";
    

    现在在控制器内部

    use PHPMailer\PHPMailer\PHPMailer;
    

    更多信息参考:https://stackoverflow.com/a/15244577/7296317

    CodeIgniter 3.x

    如果您希望CodeIgniter 使用Composer auto-loader,只需将$config['composer_autoload'] 设置为TRUEapplication/config/config.php. 中的自定义路径

    参考:https://www.codeigniter.com/user_guide/general/autoloader.html

    在你想要使用 PHP 邮件程序的文件顶部,可能需要类似以下内容:

    use PHPMailer;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多