Crash including Swiftmailer file after installing along with composer

I am a newbie using composer and after installation I am having problems including Swiftmailer files in my open php page. I've installed Swiftmailer with composer using this instruction:

php composer.phar require swiftmailer/swiftmailer @stable

      

Everything is working fine and the file is set to

./vendor/swiftmailer/swiftmailer/

      

Since the files are in the root and not in public_html, I cannot include them to start using it.

Did I make the mistake of setting Swiftmailer to this position? Do I need to change it? If it's so fast?

+3


source to share


2 answers


In short: add the following line require 'vendor/autoload.php';

to your application's bootstrap (if it doesn't already exist).

You can just include this file and you will get autoloading for free. Link: https://getcomposer.org/doc/01-basic-usage.md#autoloading


Long version:

Swiftermailer adds file lib/swift_required.php

to composer autoloader:



See https://github.com/swiftmailer/swiftmailer/blob/master/composer.json#L25

This file requires some other Swiftmailer files and registers the Swiftmailer autoloader.

The flow of execution should look something like this:

  • your app index.php
  • your bootstrap.php
  • requires Composer autoloader
  • files in the section autoload

    are loaded on every request, for exampleswiftmailer_required.php

  • Now you can use $mailer = new \Swift_Mailer();

+2


source


If you want to use stuff that Composer installed, you must include an autoloader in your code.

And since including PHP code uses the filesystem and not access to the web server, it doesn't matter where the script is in terms of being able to access it through the web server - that's good, because you don’t want to. so that every PHP script is received over the Internet.

You will need to include the Composer autoloader on one of the first lines of your script and then use whatever you installed with Composer.



include 'vendor/autoload.php'; // adjust the path depending on where your script is located.

      

This can lead to the following:

./public_html/index.php:

include '../vendor/autoload.php'; // located in  ./vendor/

$mailer = new \Swift_Mailer();

      

+2


source







All Articles