Yii2 Call to undefined method setuser - yii2

I'm using Yii2 module dektrium/yii2-user for user sign-up/sign-in and get the aforementioned error while trying to register.
PHP User Error – yii\base\ErrorException.
in C:\xampp\htdocs\hello\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\EsmtpTransport.php
How do I find out the reason for this?

You should be very careful when configuring mailer component of your app. The problem was a typo:
'viewpath' => '#app/mailer',
instead of
'viewPath' => '#app/mailer',
in /appName/config/web.php

Related

Laravel 8.5's Validation is not working and getting an error

I am just calling an API with one array parameter with the required validation. But I am getting always an error message. The weird thing is that it has been working since I created the project but it suddenly stopped working today. I also cleared config:cache and apply the composer dumpautoload command till the issue remains the same.
first, try to dump all the input data using dd helper and see if the input format is correct
so in the first line of your function dump it like below the check the output
dd( $request );
secondly, use the validate function of $request object
so instead of calling and using Validator class,
call it on the request object at the very first line of your function
$request ->validate([
'booking_data' => 'required|array'
]);
and you don't need to manually check any validation and manually return something, it will automatically throw an error if it fails

Yii2: Attach behavior to every controller

How do we attach a behavior to all web controllers in the application?
I understand this is theoretically possible with dependency injection, so I assumed something like this would work:
Yii::$container->set('yii\web\Controller', [
'as myBehavior' => [
'class' => 'app\behaviors\MyBehavior',
],
]);
however DI requires an exact class match (attaching to a parent class will not work). There is no way to know all the controller names in advance, especially when most are coming from 3rd party modules.
Is there another way to accomplish this?
EDIT: The purpose of this is to dynamically add controller filters (which are just a special type of behaviors). Therefore attaching the behavior during the EVENT_BEFORE_ACTION event is not sufficient, because it is triggered long after request filtering.
The module's (and application's) beforeAction event is triggered before the controller's version of that event. You can take advantage of that and use it to attach behaviors to current controller.
For example in your web.php config:
return [
'on beforeAction' => function() {
Yii::$app->controller->attachBehavior(
'myBehavior',
\app\behaviors\MyBehavior::class
);
},
// ... other configurations
];
Of course the disadvantage is that the behavior is not attached from the start.
NOTE: If your goal is attaching a filter to each controller, you can simply attach it to application itself instead of controllers.
Interesting problem, I must say. I could not find the simple solution for this but I have this hacky idea. You could take advantage of Yii autoloader and load your version of yii\web\Controller instead of the original one.
To do that:
Copy the original file from vendor and place it in your app
Don't change the original namespace and name.
Add your behavior config (or just the behavior's code, whatever) inside.
Add this line below in a place that will be called every time the app runs (like entry point file or bootstrap file, it must be called after vendor/yiisoft/yii2/Yii.php file is required):
Yii::$classMap['yii\web\Controller'] = ''; // replace '' with the path to your version
// of yii\web\Controller
Now, every time autoloader tries to load yii\web\Controller it will load your version instead so it should work like you want it.
The obvious con of this is that you will have to check manually if the original file has not been updated when upgrading Yii to make it up-to-date.
Child controller behavior depends on AccesControler behavior
class WorkerimgController extends OfficeController{
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
]);
}
}

How to use a model or module in both frontend and backend

Can any one guide me how to use for model in both frontend and backend in Yii2 advanced template.
Secondly what should I do if I want to use a module in both frontend and backend sides?
I have already tried to put model in common. But I am unable to access it. I am new to yii2 so detailed guide will be helpful.
In general it doesn't matter where you place it.
But in advanced template common folder exists exactly for these purposes.
For example, create User model and place it in common\models folder:
<?php
namespace common\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
...
}
No special configuration required.
Then you can use it like that:
use common\models\User;
User::find()->...
or
common\models\User::find()->...
The same with module, just place its content in common\modules\users for example. Common folder is for commonly used classes.
Also check official PHP documentation for namespaces.
You can make Models and Controllers and Views reusable via Modules approach by placing the module in common folder.
Step by step guide is below:
Make module "order" in any of the end (backend or frontend)
Create a new folder "modules" in common and cut/paste the order module from backend/frontend into this new "modules" folder
Open Module.php and rename the namespace to "common\modules\order" and rename the $controllerNamespace variable as well like this
public $controllerNamespace = 'common\modules\order\controllers';
Rename the namespace of DefaultController.php class to "common\modules\order"
Add this new module in config files (config/main.php) of both frontend and backend like this
'modules' => [
'order' => [
'class' => 'common\modules\order\Module',
],
],
'components' => [
.
.
.
Now you can access the reusable order module from both frontend and backend like this
mysite/frontend/web/index.php?r=order
mysite/backend/web/index.php?r=order

Using Codeception with Laravel and subdomains

I was hoping to use Codeception to handle a subdomain declared in Laravel 5
$router->group(array('domain' => 'admin.' . Config::get('app.host')), function()
{
Codeception appears to have an amOnSubdomain method for webdriver, but not for the Laravel 4 module.
http://codeception.com/docs/modules/WebDriver#amOnSubdomain
Is there a way to integrate this functionality with Codeception on Laravel?
I tried calling the action directly
$I->amOnAction('Auth\AuthController#showRegistrationForm');
But this throws an error
Can't be on action "Auth\AuthController#showRegistrationForm":
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
A bit confused on how to proceed.
I set an alias with the as index and it worked for me:
Route::post('/login', ['as' => 'admin.login', 'uses' => 'AuthController#postLogin']);
$I->amOnRoute('admin.login');
I also submitted an issue to the codeception repo for this method to be added. I looked into moving the method over from another module that has it already but the laravel module does some different things with it's url and history, and don't have the time at the moment to look into it more. Hopefully that method will work for you.
https://github.com/Codeception/Codeception/issues/1505

Generating an html link in CakePHP 2.2.1+?

How can I easily generate an HTML link, using the HtmlHelper class in CakePHP 2.2.1?
Imagine I declared a route that routes /finest-perfumes-ever-2012 to the Perfumes/Index Controller/Action.
I need this generated link to be:
somedomain.com/finest-perfumes-ever-2012 //Generate link HAS to obey Routes I've set.
Instead of:
somedomain.com/Perfumes/Index
The documentation doesn't seem to do much of a good job explaining how to achieve this.
Not sure if you have missed out the 2012 by accident or your question is more complicated than my answer below. Assuming the 2012 doesn't matter:
Cake makes use of quite a nifty feature - reverse routing.
If you've set up everything correctly, the following should output what you want.
<?php
Router::connect(
'/finest-perfumes-ever',
array('controller' => 'perfumes', 'action' => 'index')
);
echo $this->Html->link('View Finest Perfumes!', array('controller'=>'perfumes',
'action' => 'index'));
Providing your URL (when created using the HTML helper) has parameters that match the route exactly, the reverse routing will look up what you want the route to be, and output links accordingly.
If the 2012 is important you could probably get this working by passing parameters - there are some examples here
Define route configuration into your app/Config/routes.php at the last of all routing statements.
You can do the same by passing an argument to the action and define it in your routes.php file.
Kindly ask if it not worked for you.