Yii2 in third party software - user module not found - php

I've installed MediaWiki and am trying to achieve single sign-on, so that when my users sign into their accounts on my Yii2 system, they can also be signed in to the wiki.
So I want to 'Yii-fy' the wiki so I can fiddle about with the auth code.
With this in mind, I've followed the Yii docs and entered the following in the entry page of the wiki, which is in the 'web/wiki' directory:
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require(__DIR__ . '/../../config/web.php');
new yii\web\Application($yiiConfig); // Do NOT call run() here
The paths are fine for those files, but then I'm getting:
Previous exception:
exception 'ReflectionException' with message 'Class dektrium\\user\\Module
does not exist'
My web.php has 'user' as follows:
'user' => [
'class' => 'dektrium\user\Module',
'enableRegistration' => false,
],
How can I fix this?

Your autoloader must know how to find this class. If you're using composer, add the following in composer.json:
{
"autoload": {
"psr-4": {
"dektrium\\": "[path to dektrium folder]",
}
},
}
So, run the composer update to also update the autoloader and get the class registered.
If you are not using composer, you will have to manually register the class in your autoloader.

Related

Class not found error with composer autoload

I must be making a simple error, I have not used Composer before now. I have followed the instructions on the GitHub page, but I'm getting a Class 'ZCRMRestClient' not found error when I load the page.
composer.json:
{
"require": {
"zohocrm/php-sdk": "^2.0"
}
}
PHP is
require 'vendor/autoload.php';
$configuration = array(
'client_id' => '1000.***',
'client_secret' => '***',
'redirect_uri' => '***',
'currentUserEmail' => '***',
);
ZCRMRestClient::initialize($configuration);
$contacts = ZCRMRestClient::getModule(“Contacts”);
echo "<pre>\n";
print_r($contacts);
echo "\n</pre>";
I've tried \ZCRMRestClient::initialize($configuration) but that hasn't helped.
There is new sdk 3.0 available, and the old sdk 2.0 is moved to:
"require": {
"zohocrm/php-sdk-archive": "^2.0"
}
I'm going to assume you've executed the composer require zohocrm/php-sdk command, or potentially added the requirement straight into your project's composer.json by hand.
Make sure you're importing the correct namespace for the library - in this case it seems to be zcrmsdk\crm\setup\restclient\ZCRMRestClient
You should write use zcrmsdk\crm\setup\restclient\ZCRMRestClient; at the top of the file then invoke methods as you currently do.
Alternatively, you can invoke methods as in the following example: \zcrmsdk\crm\setup\restclient\ZCRMRestClient::initialize($configuration);
After that the most likely problem is that your autoload file doesn't contain a reference to your library.
composer dump-autoload should fix that (from the command line.)
And finally perhaps you are not requiring the vendor folder correctly!

Yii2 Class dektrium\user\Module does not exist

I have designed Yii2 powered blog by creating models, views and controllers but when I integrate user registration as explained in this tutorial https://code.tutsplus.com/tutorials/how-to-program-with-yii2-integrating-user-registration--cms-22974 I get:
Class dektrium\user\Module does not exist
I fail to solve this error anyone who can direct me, please.
I think you need to include the required module via composer:
"dektrium/yii2-user": "*"
Then run the "composer update" command in your console.
Mabe you need to add to config:
'user' => [
'class' => 'dektrium\user\Module',
'admins' => ['MegaAdmin'],
],
If you have it in common/config/main.php try to replace to backend/config/main.php

Integrating Laravel 5.2.* with firebase (Already tried j42)

I am facing the errors while using J42,
This is the composer.json I am using for the require element
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"laravelcollective/html": "^5.2"
I need to add firebase to my application, it is really important .
If anyone can provide me with the step by step installation along with an example it would be really great.
I have tried all sorts of possibilites
'Firebase' => J42\LaravelFirebase\LaravelFirebaseFacade::class
// `'Firebase' => J42\LaravelFirebase\LaravelFirebaseFacade
::class
even adding this class also seems an error since J42/laravel-firebase haven't mentioned it but nothing works without it.
I have already added J42/Laravel... to my composer.json but it also don't work, shows error App/Http/Controller/Firebase class not found
So if anyone could please help me in this issue, then it would be really great.
Thanks
Add the following line to your composer.json and run composer update:
{
"require": {
"j42/laravel-firebase": "dev-master"
}
}
Then add the service providers and facades to config/app.php
'J42\LaravelFirebase\LaravelFirebaseServiceProvider',
'Firebase' => 'J42\LaravelFirebase\LaravelFirebaseFacade'
Finally, you should configure your firebase connection in the config/database.php array.
Simple Access Token
'firebase' => array(
'host' => 'https://<you>.firebaseio.com/',
'token' => '<yoursecret>',
'timeout' => 10,
'sync' => false, // OPTIONAL: auto-sync all Eloquent models with Firebase?
)

Swift_Message not found error after swiftmailer installed in Yii2

I'm setting up a mailer in a project using Yii2. Swiftmailer seemed to be the best solution for this framework so I simply followed the docs.
In composer.json
"yiisoft/yii2-swiftmailer": "*"
Followed by:
composer install
In my config I have: (Yes, I just want it to save a file for now.)
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true,
],
Then I placed this into a controller:
\Yii::$app->mailer->compose()
->setFrom('from#domain.com')
->setTo('to#domain.com')
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody('<b>HTML content</b>')
->send();
My error is:
PHP FATAL - Class 'Swift_Message' not found
I believe, after some research this has to do with the autoloader / lazy loading but the information I can find is spotty. Most of it states that if using composer to install resolves the issue, and all of my troubleshooting has left me chasing my tail.
Here's my stacktrace. Thank you for your help.
Additional:
composer-asset-plugin - IS INSTALLED
vendor/composer/autoload_files.php DOES contain the line:
$vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',
I would love just close this embarrassment, but in case someone makes the same assumption as myself, i'd like to help.
Does you entry script contains 'vendor/autoload.php' require? Does it
point to the correct file? Try to manually put die('swiftmailer') at
'vendor/swiftmailer/swiftmailer/lib/swift_required.php' - it should
appear at any application run.
via (klimov-paul / yiisoft)
die('swiftmailer'); had no effect.
vendor/autoload.php is not required.
I had assumed that the other extensions already installed in the vendor folder were in use. I came to find that previous developers were not utilizing anything there and require(__DIR__ . "/../vendor/autoload.php"); was not in the execution path.

Using a non-laravel package on Laravel 4

Is it possible to include a package that was not specifically designed for L4 in the framework?
If so, how is it done? I know I need to add the package to my composer.json which adds it to the vendor folder, but can I register it somehow in the providers array? are there any other steps necessary?
I would like to use the Google checkout package originally designed for Yii
Using third party composer packages with Laravel 4
When developers create composer packages, they should map the auto-loading using PSR-0 or PSR-4 standards. If this is not the case there can be issues loading the package in your Laravel application. The PSR-0 standard is:
{
"autoload": {
"psr-0": { "Acme": "src/" }
}
}
And the PSR-4 standard:
{
"autoload": {
"psr-4": { "Acme\\": "src/" }
}
}
Basically the above is a standard for telling composer where to look for name-spaced files. If you are not using your own namespaces you dont have to configure anything else.
SCENARIO 1
PSR-0 standard following packages (with autoload classmap) in Laravel
This is a simple one, and for example i will use the facebook php sdk, that can be found:
https://packagist.org/packages/facebook/php-sdk
Step 1:
Include the package in your composer.json file.
"require": {
"laravel/framework": "4.0.*",
"facebook/php-sdk": "dev-master"
}
Step 2:
run: composer update
Step 3:
Because the facebook package uses a class map its working out of the box, you can start using the package instantly. (The code example below comes straight from a normal view. Please keep your logic out from views in your production app.)
$facebook = new Facebook(array(
'appId' => 'secret',
'secret' => 'secret'
));
var_dump($facebook); // It works!
SCENARIO 2
For this example i will use a wrapper from the instagram php api. Here there need to be made some tweaks to get the package loaded. Lets give it a try!
The package can be found here:
https://packagist.org/packages/fishmarket/instaphp
Step 1:
Add to composer .json
"require": {
"laravel/framework": "4.0.*",
"fishmarket/instaphp": "dev-master"
}
Then you can update normally (composer update)
Next try to use the package like you did with the facebook package. Again, this is just code in a view.
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$api = Instaphp::Instance(null, $instagramconfig);
var_dump($api); // Epic fail!
If you try the above example you will get this error:
FatalErrorException: Error: Class 'Instaphp' not found in ...
So we need to fix this issue. To do this we can examine the instagram composer.json, that has its autoload diffrent than the facebook php sdk had.
"autoload": {
"psr-0": { "Instaphp": "." }
}
Compared to the facebook composer.json:
"autoload": {
"classmap": ["src"]
}
(Composer handles different kinds of autoloading, from files and class-maps to PSR. Take a look at your vendor/composer/ folder to see how its done.)
Now we will have to load the class, manually. Its easy, just add this (top of your controller, model or view):
use Instaphp\Instaphp;
composer dump-autoload, and it works!
step2 (optional)
Another method is (if you dont want to use the "use" statement, you can simply tell composer to look for the files straight from your code. Just change the Instance like so:
// reference the name-spaced class straight in the code
$api = Instaphp\Instaphp::Instance(null, $instagramconfig);
var_dump($api); // It works
However I suggest using the usestatement to make it clear to other developers (and your future self) what (external) classes/packages are used in the program.
SCENARIO 3
Here we use the Laravels built in IOC container to register service providers. Please note that some packages might not be suitable for this method. I will use the same Instagram package as in scenario 2.
Quick and dirty
If you don't care about design patterns and service providers you can bind a class like this:
App::bind('Instaphp', function($app)
{
return new Instaphp\Instaphp;
});
And you resolve it like this.
App::make('Instaphp');
Quick and dirty end
If you're working on a bigger project, and you make use of interfaces you should probably abstract the bindings further.
Step 1:
Create a folder inside your app folder, for example a 'providers' folder.
app/providers
Make sure Laravel auto-loads that folder, you can pass in some additional info to composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/providers" // this was added
]
},
Now create a File inside the new folder called Instagram.php and place this inside:
<?php
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('Instaphp', function()
{
return new Instaphp\Instaphp;
});
}
}
Now run composer dump-autoload again, and you can use the package. Note that the instagram package has a final private function __construct(), this means you cannot use that package outside the original class without changing the construct method to public. I'm not saying this is a good practice, and i suggest to use the scenario 2, in the case of the instagram package.
Anyway, after this you can use the package like this:
$instagramInstance = App::make('Instaphp');
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$instagram = new $instagramInstance();
$userfeed = $instagram->Users->feed($instagramconfig);
var_dump($userfeed); // It works!
Add "tvr/googlecheckout": "dev-master" this to your composer.json.
Run composer install, then you can use the IoC container. Some code examples can be found in the official docs for Laravel 4: http://four.laravel.com/docs/ioc#basic-usage

Categories