Search code examples
phplaravelcomposer-phppackagist

Use TermExtractor in Laravel 5


I would like to use a library called term-extractor (https://packagist.org/packages/fivefilters/term-extractor) in Laravel 5. I tried to install it via composer with

composer require "fivefilters/term-extractor:*" 

I think, this step was sucessfull as i found the following directory: vendor/fivefilters/term-extractor. As well a line was added to composer.json:

"require": {
        "laravel/framework": "5.0.*",
        "fivefilters/term-extractor": "*"
    },

But from this point I don't know how to proceed.

Is the package really available to my app? How may I use term extractor? If I try with

$extractor = new TermExtractor();

it doesn't work.

Thanks!

--

I follwed the advises given by Alan Storm - many thanks!

  1. Running composer dumpautoload

    composer dumpautoload
    Generating autoload files
    

seems to be fine.

  1. Running my code

    Whoops, looks like something went wrong.
    1/1 FatalErrorException in NewsletterController.php line 81: Class 'Fivefilters\TermExtractor' not found
    
    in NewsletterController.php line 81
    at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'Fivefilters\TermExtractor' not found', 'file' => '/Applications/MAMP/htdocs/newsletterfeed/app/Http/Controllers/NewsletterController.php', 'line' => '81')) in HandleExceptions.php line 116
    at HandleExceptions->handleShutdown()
    

where we finde in line 81

$extractor = new TermExtractor();

Any suggestions what's going wrong? Many Thanks!


Solution

  • What you've done is

    1. Configure your composer.json file to use the fivefilters/term-extractor package

    2. Used Composer to download the fivefilters/term-extractor package into your vendor folder

    What you do next depends on how well the package developers have setup their package to play nice in the modern PHP world.

    If you search for the package on packagist, you'll find the source repository for the package is at BitBucket.

    If you look at the package's composer.json file, you'll see it's setup with the following autoloader section

    "autoload": {
        "psr-0": { 
            "TermExtractor": "",
            "Tagger": "",
            "PermissiveFilter": "",
            "DefaultFilter": ""
        }
    }
    

    Without going into the full details (which is too long of an answer for StackOverflow -- see my Laravel, Composer, and the State of Autoloading), this psr-0 sections means PHP should try to autoload the "TermExtractor" class from

    vendor/fivefilters/term-extractor/TermExtractor.php
    

    Laravel 5 is setup to automatically use Compoer's autoloader library. This means, if you're writing code from a Laravel bootstrapped environment, all you should need to do is

    $object = new TermExtractor();
    var_dump(get_class($object));
    

    If that doesn't work it means

    1. The maintainers of fivefilters/term-extractor have an error in their autoloader logic

    2. Composer isn't automatically running composer dumpautoload for you

    3. You're not running your code from a Laravel bootstrapped enviornment (app/routes.php, a controller file, etc.)

    I'd start by running

    $ composer dumpautoload
    

    and seeing if that helps. If it doesn't posting the error, or what unexpected behavior happened, as well as where you're writing this code (back in your main question) will help people here help you diagnose what's wrong with your system.

    Update: Based on the updated question, the following error

    Whoops, looks like something went wrong. 1/1 FatalErrorException in NewsletterController.php line 81: Class > 'Fivefilters\TermExtractor' not found

    Makes it sound like you're trying to run your code from a file that lives in the Fivefilters namespace. i.e.

    namespace Fivefilters
    //... other code
    $extractor = new TermExtractor();
    

    That doesn't quite make sense -- based on the file the code is running from

    app/Http/Controllers/NewsletterController.php
    

    I'd expect the namespace to be App\Http\Controllers. Regardless of that, it looks like the five filters classes all live in the global namespace. This means if you want to use them from a namespaced PHP file, you'll need referrer to them either explicitly as global classes (leading \)

    $object = new \TermExtractor;
    

    Or use the use statement at the top of your file

    namespace Some/Non/Global/Namespace
    //...
    use TermExtractor;
    //...
    $extractor = new TermExtractor();