Hi, I am using the following https://github.com/jayhealey/Robots package to add another robots.txt file for each environment that I have in my application. However, this is missing one of the methods that I use in my application, which is a traversal delay i.e.
Crawl-delay: 3600
Now I have the following folder from installing this package:
provider / healey / robots / src / Healey / Robots / Robots.php and it starts as follows:
<?php namespace Healey\Robots; class Robots {....}
Now I want to extend this class so that I can successfully use the method inside it, but obviously I do not want to add the function to the provider directory, as this is undesirable. So I created the following class:
Application / Library / Helpers / AppRobots.php
and it has the following content:
<?php use Healey\Robots\Robots; class AppRobots extends Robots { public function crawlDelay($delay) { $this->addLine("Crawl-delay: $delay"); } }
Now in routes.php I have the following:
Route::get('robots.txt', function() { // If on the live server, serve a nice, welcoming robots.txt. if (App::environment() == 'production') { \Robots::addUserAgent('*'); \AppRobots::crawlDelay('3600'); } else { // If you're on any other server, tell everyone to go away. \Robots::addDisallow('*'); } return Response::make(Robots::generate(), 200, array('Content-Type' => 'text/plain')); });
Now this causes the following error:
The non-static AppRobots :: crawlDelay () method should not be called static
So, I changed the method to static as follows:
public static function crawlDelay($delay) { $this->addLine("Crawl-delay: $delay"); }
However, this results in the following error:
Using $ this, if not in the context of the object, so I updated this to use the following method:
public static function crawlDelay($delay) { $robots = new \Robots(); $robots->addLine("Crawl-delay: $delay"); }
and now I get Call to undefined method Healey\Robots\RobotsFacade::addLine()
This is the RobotsFacade file (provider / healey / robots / src / Healey / Robots / RobotsFacade.php)
<?php namespace Healey\Robots; use Illuminate\Support\Facades\Facade; class RobotsFacade extends Facade { protected static function getFacadeAccessor() { return 'robots'; } }
and this is a service provider (provider / healey / robots / src / Healey / Robots / RobotsServiceProvider.php)
<?php namespace Healey\Robots; use Illuminate\Support\ServiceProvider; class RobotsServiceProvider extends ServiceProvider { protected $defer = false; public function boot() { $this->package('healey/robots'); } public function register() { $this->app['robots'] = $this->app->share(function($app) { return new Robots(); }); $this->app->booting(function() { $loader = \Illuminate\Foundation\AliasLoader::getInstance(); $loader->alias('Robots', 'Healey\Robots\RobotsFacade'); }); } }
Any ideas how I can successfully extend this class to add an extra method as needed?
Update
Application / Library / CustomRobotsServiceProvider.php
<?php namespace MyApp\Healey\Robots; use Illuminate\Support\ServiceProvider; class CustomRobotsServiceProvider extends ServiceProvider { public function boot() { } public function register() { $this->app['robots'] = $this->app->share(function($app) { return new AppRobots(); }); } }
Application / Library / Helpers / AppRobots.php
<?php namespace MyApp\Healey\Robots; use MyApp\Healey\Robots\CustomRobotsServiceProvider; use Healey\Robots\Robots as Robots; class AppRobots extends Robots { public static function crawlDelay($delay) { $robot = new Robots(); $robot->addLine("Crawl-delay: $delay"); } }
app.php in the providers array, I have the following:
'Healey\Robots\RobotsServiceProvider', 'MyApp\Healey\Robots\CustomRobotsServiceProvider'
in an array of aliases, I have the following:
'Robots' => 'Healey\Robots\Robots',
However, this does not add the Crawl Delay line using this method:
\Robots::crawlDelay('3600');
Any ideas why this line is not written on the robots.txt route? It reaches the penalty method, but does not successfully add this line.