Laravel扩展了从Composer安装的供应商类 [英] Laravel extend a vendor class installed from composer

查看:67
本文介绍了Laravel扩展了从Composer安装的供应商类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下软件包 https://github.com/jayhealey/Robots 添加应用程序上每个环境都有一个不同的robots.txt文件.但这缺少我在我的应用程序上使用的一种方法,即爬网延迟,即

Hi I am using the following package https://github.com/jayhealey/Robots to add a different robots.txt file per environment I have on my application. However this is missing one method I am using on my application which is the crawl delay i.e.

Crawl-delay: 3600

现在我从该软件包的composer安装中获得以下文件夹:

Now i have the following folder from the composer install of this package:

vendor/healey/robots/src/Healey/Robots/Robots.php,其开始如下:

vendor/healey/robots/src/Healey/Robots/Robots.php and this starts as so:

<?php namespace Healey\Robots;

class Robots
{....}

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

Now I want to be able to extend this class so i can use the method within it successfully, but obviously do not want to add the function within the vendor directory as this is undesirable. So I have created the following class:

app/lib/Helpers/AppRobots.php

app/lib/Helpers/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中,我有以下内容:

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 throws the following error:

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

Non-static method AppRobots::crawlDelay() should not be called statically

因此我将方法更改为static,如下所示:

So i've changed the method to static as follows:

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

但是这会引发以下错误:

However this then throws the following error:

在不在对象上下文中时使用$ this,因此我将其更新为使用以下方法:

Using $this when not in object context so I updated this to use the following method:

/**
 * 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)

This is the RobotsFacade file (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)

and this is the service provider (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');
        });
    }

}

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

Any ideas how I can successfuly extend this class so I can add an additional method as required?

app/lib/CustomRobotsServiceProvider.php

app/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();
    });
  }
}

app/lib/Helpers/AppRobots.php

app/lib/Helpers/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中,我具有以下内容:

app.php in the providers array, I have the following:

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

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

in the aliases array I have the following:

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

但是,这并未使用此方法添加抓取延迟"行:

However this is not adding the Crawl Delay line using this method:

        \Robots::crawlDelay('3600');

有什么想法为什么不将这一行写到robots.txt路由中?它已达到方法的要求,但未成功添加此行.

Any ideas why this line isn't been written to the robots.txt route? It is reaching the method fine but not successfully adding this line.

推荐答案

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

You'll need to create your own service provider and overwrite the "robots" service there so that it uses your class, not the base one.

  1. 创建服务提供商

  1. Create a service provider

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();
    });
  }
}

  • 在您的config/app.php中注册服务提供商

  • Register service provider in your config/app.php

    'providers' => array(
       ... some other providers
       'Your\Namespace\CustomRobotsServiceProvider'
    ),
    

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

    Make sure your provider is registered after the RobotsServiceProvider so that your service overwrites the original one, not vice versa.

    这篇关于Laravel扩展了从Composer安装的供应商类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆