full namespace vs registered class alias - laravel

my problem is in tinker i tried to set value with \Redis::set("name","john")
in redis and got this error:
PHP Error: Non-static method Redis::set() cannot be called statically in Psy Shell code on line 1
but when using for example \Illuminate\Support\Facades\Redis::set("name","john") it works and everything is ok.
with this registeration in app.php by default:
'aliases' => [
'Redis' => Illuminate\Support\Facades\Redis::class,
],
so
\Illuminate\Support\Facades\Redis::set("name","MOHSEN") = \Redis::set("name","john")
what is the problem?

Laravel uses the predis extension, and php also installs and uses the phpredis extension. Since both of the above redis extensions use Redis as the namespace, namespace conflicts are caused. Therefore in your Laravel project, you used predis before, but now switch to phpredis. Then the static method in the previous code will report an error.
Solutions to solve the cause
method one: In Laravel, since predis is an extension made based on PHP, it can be added through composer to facilitate integration with Laravel. Therefore, you can avoid conflicts by annotating the extension of phpredis in php.ini.
;extension = redis.so
Disadvantages:
predis maintenance has been cancelled, Laravel has started from version 6, it is recommended to use phpredis.
Method 2: In the code, clearly indicate Redis
use Illuminate\Support\Facades\Redis;
Method 3: change aliases Also stable
in config\app.php
'Redis' => Illuminate\Support\Facades\Redis::class
Replace with
'The desired name' => Illuminate\Support\Facades\Redis::class
And finally, use the desired name;
The second method is a more appropriate option. You can replace it with a small amount of code.

Related

Need install Laravel-ide-helper for VS Code

I use VS Code, not use PHP Storm. Should I install 'laravel-ide-helper'?
You can require this package with composer using the following command:
composer require --dev barryvdh/laravel-ide-helper
Then if you are using Laravel versions older than 5.5, add the service provider to the providers array in config/app.php:
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
In Laravel, instead of adding the service provider in the config/app.php file, you can add the following code to your app/Providers/AppServiceProvider.php file, within the register() method:
public function register(){
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
This basically allow your application to load the Laravel IDE Helper on non-production environments.
It does help with code completion (automatically importing classes, showing methods, etc..). It works with VSCode the same as it does for PHPStorm as far as I can tell. The 'ide-helper-models' also appears to work the same. I'm unsure what the 'ide-helper:meta' does, but the docs only mention support for PHPStorm.

Integrate CHARGEBEE library to LARAVEL

Im starting using LARAVEL last version 7.12 and I'm having an issue trying to integrate CHARGEBEE library to make request to Chargebee api.
I was reading that I can install packages with composer, I did it:
composer require chargebee/chargebee-php:'>=2, <3'
Doing that now I have downloaded chargebee lib here: /vendor/chargebee/chargebee-php/
Now also I saw This stack overflow question here:
That for the user to correctly use this Library-Package I need to create a ServiceProvider so I did:
php artisan make:provider ChargeBeeServiceProvider
then I really don't know how to write the REGISTER() function here, I added also this line: App\Providers\ChargebeeServiceProvider::class, to /config/app.php to 'providers'
ChargebeeServiceProvider
Right now I have a controller: /app/http/controllers/PortalController and I'm trying to use this:
ChargeBee_Environment::configure("sitename","apikeyvalue");
$all = ChargeBee_Customer::all(array( "firstName[is]" => "John",
"lastName[is]" => "Doe", "email[is]" => "john#test.com" ));
foreach($all as $entry){
$customer = $entry->customer();
$card = $entry->card();
}
PortalController
BUT on the frontend it's giving me an error:
Error Class 'App\Http\Controllers\ChargeBee_Customer' not found
Not really sure how i can use this custom Chargebee library here on Laravel.
can somebody please help to integrate in the correct way ?
Thanks!
The real problem here is that ChargeBee_Environment class doesn't exists.
It should be Chargebee\ChargeBee\Environment. And the Models are in their own folder, for example the Portal is ChargeBee\ChargeBee\Models\PortalSession
WPTC-Troop's answer provides some great information on the issue of using a service provider vs library directly, but in the end you need to call the correct classes.
My working code for creating a portal session is below. It would be similar for a Customer object:
\ChargeBee\ChargeBee\Environment::configure("test-site","test_api_key");
$result = \ChargeBee\ChargeBee\Models\PortalSession::create(["customer" => ["id" => "AzZlwTSSVw9871E9g"]]);
$portalSession = $result->portalSession();
$output = $portalSession->getValues();
return $output;
Two things, you can use any third party library as Service Provider(DI) in Laravel or use it directly where ever you needed(mostly in controller)
Not just third party library you can ease the instance creation of class etc in Service Provider. Basically it's for dependency injection, Check IoC in general you'll understand it better.
You tried/mixed both in your case and didn't completely use any single approach.
You've created a Service Provider called ChargeBeeServiceProvider but it doesn't return/bind any resource or no API initialization done in register method. This is where you register and make use of DI. You can bind in properties as well. Here is the official doc to it.
You tried to use directly by initializing ChargeBee_Environment and use ChargeBee_Customer but you didn't mention where to look for this class that is the reason you get the error(Error Class 'App\Http\Controllers\ChargeBee_Customer' not found). I mean compiler will look for ChargeBee_Environment from the same local space but ChargeBee_Environment is available from global scope.
Simple solution to your same code. Add \ to the class to look from global scope or make use of use statement in the top of your file similar to import in java but still we need to require/include the file in php but don't worry, chargebee make use of autoloader(Check here -> composer.json) so it will be loaded automatically in both approach.
\ChargeBee_Environment::configure("sitename","apikeyvalue");
$all = \ChargeBee_Customer::all(array( "firstName[is]" => "John",
"lastName[is]" => "Doe", "email[is]" => "john#test.com" ));
Try the above and let me know. Answered from mobile device. Code is not tested fully(Just copied your and added \ to it).
Excuse brevity :)
I have a solution to use ChargeBee and Laravel. Don't need any: register provider, add to aliases or add path to autoload in composer.json by file path. Just add in use classes that you need in code your Service or your Controller. Like use ChargeBee_Environment and other.
use ChargeBee_Environment;
use ChargeBee_Customer;
ChargeBee_Environment::configure("sitename","apikeyvalue");
$all = ChargeBee_Customer::all(array( "firstName[is]" => "John", "lastName[is]" => "Doe", "email[is]" => "john#test.com" ));
foreach($all as $entry){
$customer = $entry->customer();
$card = $entry->card();
}

Can't force https on laravel project

I need to use facebook api, and it requires me to have https, so I want to force my laravel project to use it.
With Laravel I use react scafffolding also I run project on Xampp
I already tried
setting 'url' => 'https://localhost' in config/app.php
putting HTTPS=true APP_URL=https://localhost in .env
putting \Illuminate\Support\Facades\URL::forceScheme('https'); if($this->app->environment('production')) {
\URL::forceScheme('https');
} in boot method at appServiceProvider.php
my url seems not to change, it always says localhost:8000
My laravel project is not small, I don't know how mutch code is needed to reprocuce same issue, inform, and I'll provide anything

Route::controller() alternative in Laravel 5.3+

I just upgraded from Laravel 5.2 to 5.3. I am using Laravel-DataTables package for several tables in my application.
After upgrade when I run artisan serve I'm receiving:
[BadMethodCallException]
Method controller does not exist.
I've tracked the issue down to this piece of code in my routes.php (now web.php)
Route::controller('datatables', 'ProfileController', [
'anyOrders' => 'datatables.dataOrders',
'anyProperties' => 'datatables.dataProperties',
]);
This is the suggested way to route the queries for DataTables Documentation.
Was the Route::controller() deprecated, and what is the alternative to for these routes?
The explicit routes will be:
Route::get('/datatables/orders', array('middleware' => 'auth', 'uses' => 'ProfileController#anyOrders'))->name('datatables.dataOrders');
Route::get('/datatables/properties', array('middleware' => 'auth', 'uses' => 'ProfileController#anyProperties'))->name('datatables.dataProperties');
I had the same issue as you, and none of the alternatives (explicit declaration or publishing) was good enough. There were also some alternatives which required changing too much code.
This is why I wrote a class called AdvancedRoute, which serves as a drop in replacement.
It can be used by simply replacing Route::controller with AdvancedRoute::controller like this:
AdvancedRoute::controller('users','UserController');
Full information how to install and use find at the GitHub repo at:
https://github.com/lesichkovm/laravel-advanced-route
Hope you find this useful.
https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file. This will likely be extracted into a package.
You can use resource().
Route::resource('users','UserController');
Note: the "get" prefix is not needed. getIndex() = index()

Alternative to APC in Yii2

I developed a website using Yii2 Framework, and while I didn't explicitly use the Cache features, I guess it does do some things using APC as default.
The client where I published the website had uninstalled APC, since it is deprecated since v5.5 and refuse to install the extension.
My client now keeps receiving an 'unable to load dynamic library - apc.so' every time they try to save or delete a record to the database, not read.
I tried to clear the cache sub folder under the runtime folder in hopes that the website will use whatever system is available, but the error still creeps up.
They are using opcache. How can I reconfigure yii to use opcache and prevent the inability to find apc.so error?
EDIT:
This is what I have under components.
'cache' => [
'class' => 'yii\caching\FileCache',
],
If your cache is properly configured you should find something like this in the configuration file:
'components' => [
'cache' => [
'class' => 'yii\caching\ApcCache',
],
],
Now, AFAIK OpCache does not require configuration on the code level so you don't have to replace this config with something OpCache specific but there are direct cache calls in your code (hence the error) so you may want to use some available cache component anyway. In case you don't want to use any new cache component and at the same time you don't want to remove cache calls in your code use DummyCache:
'cache' => [
'class' => 'yii\caching\DummyCache',
],
EDIT:
It looks like it is not a case of Yii 2 configuration, more like a PHP configuration. Look for "unable to load dynamic library - apc.so". Probably APC is still in the PHP configuration but the library has been removed.
Related questions:
https://serverfault.com/questions/623520/php-startup-unable-to-load-dynamic-library-usr-lib-php5-20100525-apc-so
PHP APC Error Loading apc.so
APC - Unable to load dynamic library

Resources