Closed atomixstar closed 5 years ago
One of these classes needs to implements Spatie\Crawler\CrawlObserver
- either class in App\Abstracts
will do.
But in vendor/spatie/crawler/src/CrawlObserver.php is an interface not an abstract class
Right. That's why you implements
instead of extends
it. That's the purpose of interfaces - for classes to implement.
I am sorry but I think that the documentation on the product page is simply misleading because it tells to extend
rather than implements
. I found this script very hard to implement unfortunately due to poor documentation.
At the moment it reads:
use Spatie\Crawler\Crawler;
Crawler::create()
->setCrawlObserver(<class that extends \Spatie\Crawler\CrawlObserver>)
->startCrawling($url);
Feel free to submit a PR to fix that typo.
I agree the documentation is poor
Hello, I am trying to setup the crawler on laravel but I cannot extend the abstract class, I am sure I am missing something.
App\Abstracts\CrawlLogger.php
`<?php namespace App\Abstracts; use Psr\Http\Message\UriInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\RequestException;
class CrawlLogger extends CrawlObserver { /* @var string / protected $observerId; public function __construct(string $observerId = '') { if ($observerId !== '') { $observerId .= ' - '; } $this->observerId = $observerId; } /**
App\Abstracts\CrawlObserver.php
`<?php
namespace App\Abstracts;
use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use App\Abstracts\CrawlLogger;
abstract class CrawlObserver { /**
@param \Psr\Http\Message\UriInterface $url */ public function willCrawl(UriInterface $url) {
}
/**
@param \Psr\Http\Message\UriInterface|null $foundOnUrl */ abstract public function crawled( UriInterface $url, ResponseInterface $response, UriInterface $foundOnUrl = null );
/**
@param \Psr\Http\Message\UriInterface|null $foundOnUrl */ abstract public function crawlFailed( UriInterface $url, RequestException $requestException, UriInterface $foundOnUrl = null );
/**
Called when the crawl has ended. */ public function finishedCrawling() {
} }`
Controller:
`.....
use stdClass; use GuzzleHttp\Psr7\Uri; use Spatie\Crawler\Crawler; use Spatie\Crawler\CrawlProfile; use Psr\Http\Message\UriInterface; use Spatie\Browsershot\Browsershot; use Spatie\Crawler\CrawlSubdomains; use Spatie\Crawler\CrawlInternalUrls; use Spatie\Crawler\Exception\InvalidCrawlRequestHandler;
use App\Abstracts\CrawlLogger;
class TestController extends Controller {
public function index() {
}`
I am getting this error:
Type error: Argument 1 passed to Spatie\Crawler\Crawler::setCrawlObserver() must be an instance of Spatie\Crawler\CrawlObserver, instance of App\Abstracts\CrawlLogger given, called in
I am trying to learn how is working and I have search around the web related to extend of classes, but still cannot understand what I am doing wrong.
Thank you :)