Monday, February 12, 2018

Isolate external php code in Laravel - development

Isolate external php code in Laravel

I need to integrate Slider Revolution editor into my Laravel (5.5) app.

I’ve put the editor in public/revslider/ folder to be able to use the visual editor. I also created a helper class to “communicate” with it and be able to use it inside my Blade views:

namespace AppHelpers;

include( public_path('revslider/embed.php') );

class Slider{

/**
 * This function is called where you want to put your slider
 */
public static function make($slider){
    return RevSliderEmbedder::putRevSlider( $slider );
}

/**
 * This function is called inside <HEAD> tag to include all 
 * SR assets (js/css/font files)
 */
public static function head(){
    return RevSliderEmbedder::headIncludes(false); 
}

}

The SR’s PHP code does not use namespaces. In fact it is a strange mix of Code Igniter, WordPress and vanilla php.

The problem is it is trying to declare a translation function __(...):

if( ! function_exists('__'))
{
    function __($string = '')
    {
       ....
    }
}

and since there is already such Laravel’s helper function, it does not redeclare it and tries to use Laravel’s __() function. And that obviously causes errors.

I temporarily managed to fix this problem by changing the name of SR’s __() function (and all references to it). But of course it is not a best way to solve this problem, since I will be unable to use SR’s automatic updates or will be forced to do these changes after every update.

So my questions are:

  1. Is there any good way of integrating such “bad” code into your project, invoking it safely without conflicts? Is there any way of isolating such code and avoid clashes? By “bad code” I mean code that does not follow strict OOP/PSR rules present in projects like Laravel.

  2. What is the best way to include “external” PHP code? I’ve just used plain include() inside of my helper class’ file, but is there a better/cleaner way? Like, I don’t know, loading it through composer?



from Laravel Questions and Answers https://laravelquestions.com/laravel/isolate-external-php-code-in-laravel/
via Lzo Media

1 comment: