Laravel扩展了从composer安装的供应商类

001*_*221 9 extend laravel-4

嗨我正在使用以下软件包https://github.com/jayhealey/Robots为我的应用程序中的每个环境添加不同的robots.txt文件.然而,这是我在我的应用程序上使用的一种方法,即爬行延迟,即

Crawl-delay: 3600

现在我从这个包的composer安装中得到以下文件夹:

vendor/healey/robots/src/Healey/Robots/Robots.php,这样开始:

<?php namespace Healey\Robots;

class Robots
{....}
Run Code Online (Sandbox Code Playgroud)

现在我希望能够扩展这个类,这样我就可以成功地使用它中的方法,但显然不希望在vendor目录中添加该函数,因为这是不可取的.所以我创建了以下类:

应用程序/ LIB /助手/ AppRobots.php

这有以下内容:

<?php
use Healey\Robots\Robots;
class AppRobots extends Robots {

    /**
     * Add crawl Delay to robots.txt.
     *
     * @param string $delay
     */
    public function crawlDelay($delay)
    {
        $this->addLine("Crawl-delay: $delay");
    }

}
Run Code Online (Sandbox Code Playgroud)

现在在routes.php我有以下内容:

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'));
});
Run Code Online (Sandbox Code Playgroud)

现在这会引发以下错误:

不应静态调用非静态方法AppRobots :: crawlDelay()

所以我把方法改为静态如下:

public static function crawlDelay($delay)
{
    $this->addLine("Crawl-delay: $delay");
}
Run Code Online (Sandbox Code Playgroud)

但是,这会引发以下错误:

不在对象上下文中时使用$ this所以我更新了它以使用以下方法:

/**
 * Add crawl Delay to robots.txt.
 *
 * @param string $delay
 */
public static function crawlDelay($delay)
{
    $robots = new \Robots();
    $robots->addLine("Crawl-delay: $delay");
}
Run Code Online (Sandbox Code Playgroud)

现在我明白了 Call to undefined method Healey\Robots\RobotsFacade::addLine()

这是RobotsFacade文件(vendor/healey/robots/src/Healey/Robots/RobotsFacade.php)

<?php namespace Healey\Robots;

use Illuminate\Support\Facades\Facade;

class RobotsFacade extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'robots'; }
}
Run Code Online (Sandbox Code Playgroud)

这是服务提供商(vendor/healey/robots/src/Healey/Robots/RobotsServiceProvider.php)

<?php namespace Healey\Robots;

use Illuminate\Support\ServiceProvider;

class RobotsServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        $this->package('healey/robots');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    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');
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

任何想法如何成功扩展这个类,以便我可以根据需要添加一个额外的方法?

更新

应用程序/ LIB/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();
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

应用程序/ LIB /助手/ AppRobots.php

<?php
namespace MyApp\Healey\Robots;
use MyApp\Healey\Robots\CustomRobotsServiceProvider;
use Healey\Robots\Robots as Robots;
class AppRobots extends Robots {

    /**
     * Add crawl Delay to robots.txt.
     *
     * @param string $delay
     */
    public static function crawlDelay($delay)
    {
        $robot = new Robots();
        $robot->addLine("Crawl-delay: $delay");
    }

}
Run Code Online (Sandbox Code Playgroud)

在provider数组中的app.php,我有以下内容:

    'Healey\Robots\RobotsServiceProvider',
    'MyApp\Healey\Robots\CustomRobotsServiceProvider'
Run Code Online (Sandbox Code Playgroud)

在别名数组中我有以下内容:

     'Robots'         => 'Healey\Robots\Robots',
Run Code Online (Sandbox Code Playgroud)

但是,这不是使用此方法添加Crawl Delay行:

        \Robots::crawlDelay('3600');
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么这条线没有被写入robots.txt路线?它正在达到方法,但没有成功添加此行.

jed*_*ylo 6

您需要创建自己的服务提供商并覆盖"机器人"服务,以便它使用您的类,而不是基类.

  1. 创建服务提供商

    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();
        });
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在config/app.php中注册服务提供商

    'providers' => array(
       ... some other providers
       'Your\Namespace\CustomRobotsServiceProvider'
    ),
    
    Run Code Online (Sandbox Code Playgroud)

确保您的提供商在RobotsServiceProvider之后注册,以便您的服务覆盖原始服务,反之亦然.