Framework Integration » Laravel

Framework Integration

Laravel

Easily integrate Roach into any Laravel application.

This page will walk you through the steps to install Roach into your Laravel projects.

The Laravel adapter mostly provides the necessary container bindings for the various services Roach uses, as well as making certain configuration options available via a config file. To learn about how to actually start using Roach itself, check out the rest of the documentation.

Installing the Laravel Adapter

Note: The Laravel integration for Roach requires Laravel 9.x.

Instead of installing the core Roach package, we are going to install Roach’s Laravel adapter.

composer require roach-php/laravel

We can also publish the configuration file that comes with the package.

php artisan vendor:publish --provider='RoachPHP\Laravel\RoachServiceProvider'

This will publish a roach.php configuration file to our app’s config folder.

Available Commands

The Laravel adapter of Roach registers a few Artisan commands to make out development experience as pleasant as possible.

Generating new Spiders

To quickly stub out a new spider, we can use the roach:spider Artisan command.

php artisan roach:spider LaravelDocsSpider

This command will create a new spider with the provided name inside our app’s Spider directory. Check out the section about getting started with spiders to learn about how to proceed from this point.

Starting the REPL

Roach ships with an interactive shell (often called Read-Evaluate-Print-Loop, or Repl for short) which makes prototyping our spiders a breeze. We can use the provided roach:shell command to launch a new Repl session.

php artisan roach:shell https://roach-php.dev/docs/introduction

Check out the shell documentation to learn more.

Running a Spider

To start a run for a given spider directly from the CLI, we can use the roach:run command and pass it the name of our spider.

php artisan roach:run MySpider

By default, this will prefix the name of our spider with the default namespace configured by the default_spider_namespace option.

It’s also possible to pass in a relative namespace.

php artisan roach:run Secret\\MySpider

Assuming the default_spider_namespace is App\Spiders, this would try and find the corresponding spider class at App\Spiders\Secret\MySpider.

If we pass in the fully qualified name of an existing spider class, the default namespace will get ignored.

php artisan roach:run App\\Domain\\Scraping\\Spiders\\MySpider

Configuration

We can change Roach’s default settings in the roach.php config file.

Changing the Default Namespace

Both the roach:run and roach:spider commands assume that our spider classes live in the App\Spiders namespace. To change this, we can update the default_spider_namespace configuration option.

/*
|--------------------------------------------------------------------------
| Default Spider Namespace
|--------------------------------------------------------------------------
|
| The default namespace the `roach:run` and `roach:spider` commands use
| to determine the namespace of spider classes. This should not contain
| leading or trailing backslashes.
|
*/
'default_spider_namespace' => 'App\Spiders',