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

查看:16
本文介绍了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.

抓取延迟:3600

现在我从这个包的 composer install 中获得了以下文件夹:

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 HealeyRobots;

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 HealeyRobotsRobots;
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

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

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 HealeyRobotsRobotsFacade::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 HealeyRobots;

use IlluminateSupportFacadesFacade;

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 HealeyRobots;

use IlluminateSupportServiceProvider;

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 = IlluminateFoundationAliasLoader::getInstance();
            $loader->alias('Robots', 'HealeyRobotsRobotsFacade');
        });
    }

}

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

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 MyAppHealeyRobots;
use IlluminateSupportServiceProvider;
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 MyAppHealeyRobots;
use MyAppHealeyRobotsCustomRobotsServiceProvider;
use HealeyRobotsRobots 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");
    }

}

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

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

    'HealeyRobotsRobotsServiceProvider',
    'MyAppHealeyRobotsCustomRobotsServiceProvider'

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

in the aliases array I have the following:

     'Robots'         => 'HealeyRobotsRobots',

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

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 IlluminateSupportServiceProvider;
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
       'YourNamespaceCustomRobotsServiceProvider'
    ),
    

  • 确保您的提供程序在 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天全站免登陆