3rd Party Library

Hi all,

I've had a search and can't find exactly what I'm after, hence the new thread.

I am building a site where I need to include 3rd party libararies such as those to do things with Amazon's AWS or processing payments through Stripe for instance.

Such classes from these libraries would be called within single page controllers and therefore it seems like overhead to build a package as I would normally just place the libraries under /libraries/3rdParty pre 5.7 and use the loader.

Could someone provide me a little guidance on something that I may have missed or a best-practice I should be learning/adopting to help with this pattern I've used many times pre 5.7.



View Replies:
MrKDilkington replied on at Permalink Reply
Hi dbaggs,

Have you looked over the package documentation?

"Advanced: Adding Custom Code Libraries and Third Party Libraries in a Package"

I've found that there are problems with third party libraries that don't use namespaces and rely on being included. I would be interested in how to deal with them.
dbaggs replied on at Permalink Reply
Hi and thanks for your reply.

Yes - I did read through that article but as I mentioned above, my 5.6 brain is resisting putting things into a package as that feels like an overhead in this case.

I have just re-read the following autoloading article https://www.concrete5.org/documentation/developers/5.7/environment/a... and am about to try creating my own application/src directory and use the appropriate namespace.

I'll post back here to let you know how I get on.

dbaggs replied on at Permalink Reply
ok - like with the way things work when someone looks over your shoulder, posting this thread appears to have given me greater focus to resolve the issue.

After some initial hesitance to use composer (as I'm a little old school and want to do more work first, understand the problem something like composer solves, and then use it), I realised that it was the answer.

I added a composer.json file to my application folder, which contained the following for me:

    "require": {
        "stripe/stripe-php": "2.*"

After that, I installed composer to the application folder as per the instructions here -https://getcomposer.org/doc/00-intro.md...

curl -sS https://getcomposer.org/installer | php

I then ran composer

php composer.phar install

This created an application/vendor directory and the final step was to include the autoload.php generated within.

I chose to add this to the /application/bootstrap/autoload.php file:

<?php defined('C5_EXECUTE') or die('Access Denied.');
// My Addition
require_once(DIR_APPLICATION . '/vendor/autoload.php');
// My Addition
 * ----------------------------------------------------------------------------
 * Load all composer autoload items.
 * ----------------------------------------------------------------------------
if (!@include(DIR_BASE_CORE . '/' . DIRNAME_VENDOR . '/autoload.php')) {
    die('Third party libraries not installed. Make sure that composer has required libraries in the concrete/ directory.');

I would appreciate it if others could guide me on whether this seems right. It feels like it mirrors the core and also reflects the advice for packages.
MrKDilkington replied on at Permalink Reply
Thank you for documenting your process.

I forgot to include this link in my other reply. This was written by one of the core team members. This might be what you are looking for.

"concrete5 Composer Package Example
This package is an example for how a concrete5 package that uses composer should be structured."
dbaggs replied on at Permalink Reply
MrKDilkington replied on at Permalink Reply

Using your example, I was able to use libraries in blocks and single pages in the application and packages directories.

When trying to use a library in a single page controller though, they were not found.

Have you been able to use a library in a single page controller?

Also, here is another autoloading/composer post I had bookmarked. I think this is what you are looking for.

It wasn't working because I was missing a backslash (the backslash creates an object of the class defined in global scope/global space).
Example: this works in a single page controller
$mpdf = new \mPDF();
Example: this does not work in a single page controller
$mpdf = new mPDF();