开发者

Importing external php files in Yii

开发者 https://www.devze.com 2023-03-22 21:07 出处:网络
I\'ve got some files using raw php (including config开发者_如何学运维 files) that\'s used for the automatic mailing stuff in my server.

I've got some files using raw php (including config开发者_如何学运维 files) that's used for the automatic mailing stuff in my server.

Suppose inside this file I define a couple of constants (define(...)) and an array filled with database connection info, like user, host and so).

The website is done using Yii, so it also uses a config file.

These raw files can be placed anywhere (inside protected, outside, at the same level of index.php, whatever).

Now the problem comes that I've got a different configuration file (and different users/password for databases, and so) outside Yii, but I need to use it in some places inside Yii, too.

Is there a clear way to import these files to a controller? I've done it placing them inside extensions, but the raw functions didn't work from there.


The best approach would be to see if you can put your custom code into class files and put those in the components directory or similar and convert your files to classes (if they aren't already). That way you can get at your data without a lot of mixing of code which is going to be hard to maintain.


Simple approach will be to place the files in extensions and add the path of the extensions to your yii configuration. Then make a controller and call methods from its actions. Lets consider an example of swiftmailer. Here is a custom controller you can use.

class mailerController extends Controller{

public function actions()
{
    return array(
         //can add other actions here
    );
}
    public function actionIndex(){

        echo "use mailer?e=<email>&m=<message>&sub=<subject> to send mail from the site to the email address";

    }


    public static function actionSendMail($e,$m,$sub){  

        $content = $m ; // can use a template and then assign to content 
        $SM = new SwiftMailer(); //the external method, should be present in include path
        // Get config
        $mailHost = Yii::app()->params['mailhost'];
        $mailPort = 25; // Optional
        $Transport = $SM->smtpTransport($mailHost, $mailPort);
        $Mailer = $SM->mailer($Transport);
        $Message = $SM
            ->newMessage($sub)
            ->setFrom(Yii::app()->params['sitemail'])
            ->setTo($e)
            ->addPart($content, 'text/html');
        return ( $Mailer->send($Message));
    } }

once your controller is ready, it can be called like

yoursite.com/mailer?e=<email>&m=<message>&sub=<subject>
0

精彩评论

暂无评论...
验证码 换一张
取 消