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

时间:2015-07-15 16:15:05

标签: laravel-4 extend

您好我正在使用以下软件包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
{....}

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

应用程序/ 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");
    }

}

现在在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'));
});

现在抛出以下错误:

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

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

public static function crawlDelay($delay)
{
    $this->addLine("Crawl-delay: $delay");
}

然而,这会引发以下错误:

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

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

现在我得到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'; }
}

这是服务提供商(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');
        });
    }

}

我有什么想法可以成功扩展这个类,以便我可以根据需要添加其他方法?

更新

应用程序/ 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();
    });
  }
}

应用程序/ 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");
    }

}
提供者数组中的

app.php,我有以下内容:

    'Healey\Robots\RobotsServiceProvider',
    'MyApp\Healey\Robots\CustomRobotsServiceProvider'

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

     'Robots'         => 'Healey\Robots\Robots',

但是,这并没有使用此方法添加Crawl Delay行:

        \Robots::crawlDelay('3600');

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

1 个答案:

答案 0 :(得分:6)

您需要创建自己的服务提供商并覆盖&#34;机器人&#34;在那里服务,以便它使用你的课程,而不是基础课程。

  1. 创建服务提供商

    function removeSpecificNode(el, index) {
        var children = el.children;
        if(children.length > 0) {
            el.removeChild(children[index]);
        }
    }
    
  2. 在config / app.php中注册服务提供商

    function removeNodesToOffset(el, offset) {
        var children = el.children;
        for(var i=children.length-1;i>=0+offset;i--)
            el.removeChild(children[i]);
    }
    
  3. 确保您的提供商在RobotsServiceProvider之后注册,以便您的服务覆盖原始服务,反之亦然。

相关问题