wiwa1978
wiwa1978

Reputation: 2707

Laravel adding normal PHP package

I have a new Laravel 5.6 application. I wanted to see if I could an existing library (see link). The idea is that I could use the controller to parse the fit file and display the data in a view.

I have added the package adriangibbons/php-fit-file-analysis in my composer.json file.

Then the documentation mentions:

<?php
    require __DIR__ . '/vendor/autoload.php';  // this file is in the project's root folder
    $pFFA = new adriangibbons\phpFITFileAnalysis('fit_files/my_fit_file.fit');
?>

How can this be done in Laravel? Normally I would add something in the 'providers' section of my app.php but don't know how I can add the above snippet in there.

Any ideas?

Upvotes: 0

Views: 353

Answers (1)

jfadich
jfadich

Reputation: 6348

Laravel already loads the Composer autoload file so can skip the require line. Adding a provider to the providers array in app.php allows Laravel specific packages to hook into Laravel features/config. Since this package is not Laravel specific you can just use it directly in your controller.

$pFFA = new \adriangibbons\phpFITFileAnalysis('fit_files/my_fit_file.fit');

Or a slightly more "Laravel" way

add use adriangibbons\phpFITFileAnalysis at the top of your controller. Then in the method add:

$pFFA = new phpFITFileAnalysis(storage_path('fit_files/my_fit_file.fit'));

Upvotes: 3

Related Questions