Module ID not appearing in Gii? - yii2

I am not getting Module ID as shown in the Youtube tutorial https://www.youtube.com/watch?v=GtHB58-JA38 , 7:33
I am a beginner in Yii, and would be thankful for the responses.
I mean, I am not able to create a CRUD specific to a module
After creating a Module, how to create CRUD for a module using Gii ?

You can set the view as backend\modules\settings\views\companies. By doing that, I got the same files as mentioned in the code file.

If you need relate to the settings module in crud for create the normal CRUD view
in the Module id you should digit
settings

#app/modules/settings/views/(view name) in view path , we will get same result

I had the same thing, you have just add an '#' on the beginning of the view path :
#backend\modules\settings\views\companies

It Worked for me
Model Class : backend\modules\students\models\Pdf
Search Model Class : backend\modules\students\models\PdfSearch
Controller Class : backend\modules\students\controllers\PdfController
View Path : #backend\modules\students\views\pdf

I think you should add View Path as:
backend\modules\settings\views

Related

Yii2 MySQL table names and HTTP404 errors

I am new to the Yii technology, but having worked in IT for many years I hoped I understood generally how software is designed. But the Yii guys have got me good.
I have been trying to understand why I am getting HTTP404 errors.
I have a Win 10 environment using MySQL, with latest versions of MySQL and PHP. I naively assumed that Yii would support the nomenclature standards for objects in the MySQL, especially table names. However it appears I am wrong, so here is a heads up for those interested.
I use all lower case characters for my table names, and where I have a table that is a link between two entities I use a name such as entity1_entity2. I use gii to generate a model from this table and the the CRUD option to generate a basic initial application.
I have the pretty URL active, so the initial URL I use is :
\localhost\Movies\movieactor
Where Movies is the website name defined to IIS (virtual directory) and movieactor is the lowercase name I used for the model and controller objects. The actual name prefix I used was MovieActor for the model and controller objects. I used movieactor for the view sub-directory.
Much to my surprise I was presented with an HTTP404 error message. I spent most of a day trying to understand what I had done wrong.
After a good sleep, I started again the next morning. I decided to try changing the name of the relevant table from movie_actor to movieactor. I then regenerated the model, and CRUD components. This time when I invoked the same URL I was pleasantly surprised to see the web application show me the data.
I have no idea what restrictions the Yii guys place on table names, no documentation that I can find, but this is my experience, and hopefully it may save others some grief.
Have I missed something or is my story correct ?
First of all in Yii there are no requirements for table names. Table or model class names are not directly related to URLs.
When you are using gii to generate model you are prompted to enter the table name. Then the model name is suggested as pascal case version of table name. For example if your table is named movie_actor the suggested model class name will be MovieActor. But you don't have to accept that, you can change it to whatever you want.
Then when you are generating CRUD it asks you to enter model class, search model class and controller class. The model class here is the MovieActor generated earlier. Search model class can be whatever you like, but what is important for URL is the controller class. There is something called controller id based on controller class name. It's a kebab case version of controller class name after removing the Controller suffix. If you name your controller as MovieActorController its controller id will be movie-actor. It's this controller id that you need to use in your url to reach that controller. So, url you have to use for this controller would be \localhost\Movies\movie-actor. You need to use same controller id as a sub-directory for your views too.
When you've decided to change table name to movieactor you've probably also generated the controller as MovieactorController. Because of that its id was movieactor and your url and views subfolder were correct.

How to have class loaded without using "use" but call it as if I used it

I have studied these 2 sources, but none of those works for me.
http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html
Yii2 - How do I AutoLoad a custom class?
I have created custom helper class which I want to include in every model, controller and view in my application. I am using Yii2 advanced app version 2.0.11, IDE I am using is PHPStorm
QUESTION:
What I want to achieve is to not use use keyword at the beggining of every class but still be able to simply call AppHelper::myMethod() in models, controllers and views.
How is that possible?
Closest I got it working was using this solution https://stackoverflow.com/a/35160997/5395463
All other solutions did not work for me. I am getting errors like:
PHP Fatal Error – yii\base\ErrorException
Class 'frontend\controllers\AppHelper' not found
when I simply dont include use commons\commands\AppHelper;
or when not using namespace as they suggest there with other settings:
Fatal error: Uncaught exception 'yii\base\UnknownClassException'
with message 'Unable to find 'common\commands\AppHelper'
in file: C:\xampp\htdocs\domain.com\web\common/commands/AppHelper.php.
Namespace missing?' in ...
SOLUTION:
Thanks for your responses, I got it working finaly. https://stackoverflow.com/a/42330631/5395463 solution works best for me.
So I removed namespace from that class, but left it in common\commands folder, added require to frontend/web/index.php and backend/web/index.php files (not sure if I should add it to yii file in root too, I didnt, so far it is working good anyways), and changed calls of class from AppHelper::myMethod() to \AppHelper::myMethod() looks like eveything is working now.
In Yii2 You can use an explicit way adding \ to full namespaced name
\frontend\controllers\AppHelper
so you can sue your method
\frontend\controllers\AppHelper::yourMethod();
Solution for not lazy coders:
create component with your class so you can use it like \Yii::$app->my_component
if even this is too much and your IDE is better than Windows Notepad prepare macro that will print this for you
Solution for lazy coders:
Save your class in single PHP file without namespace.
Modify you entry script to include this class - i.e. for Basic Project Template it's /web/index.php; add there
require(__DIR__ . '/path/to/MyClass.php');
For Advanced Project Template modify it properly.
Now you can use your class by calling it like \MyClass (yes, \ is required and yes, your IDE probably will modify it anyway to MyClass with use MyClass; added at the beginning.

How to log in Cakephp vendor Table class?

I try to debug a EmailQueueTable (extends Cake\ORM\Table) located in vendor folder, but I can't find how to get logs.
I tried $this->out() (the method is called by SenderShell extends Cake\Console\Shell)
I tried $this->log
I tried Log::write
The only solution I found is to throw an Exception...
I would like to say that $this->log works well in other classes of the app.
If someone as an idea of the problem ?
Thanks a lot !
$this->log works in classes that have included the LogTrait. This includes Cake's core View, Controller and Component classes. I'm not sure why Table doesn't also include this, but you can get the same functionality with \Cake\Log\Log::write(LogLevel::ERROR, $xxx);

Yii2 access to actions in new controllers

Comrades, I have had issues implementing yii2 basic but I'm yet to give up. I have successfully installed yii2, activated pretty url and created the .htaccess file in the root folder. The Home, About, Contact and Login urlswork fine.
i. I have created a new model, InstTypes with the CRUD. Why does http://localhost:8081/we#ss/instTypes/create return Not Found (#404)?
ii. I have also created a module instClients. I can access the index action in the DefaultControler. I have a model Insts with its CRUD under this modules. Why does http://localhost:8081/we#ss/instClients/insts/create return Not Found (#404)?
I tend to think that this could be due to the removal of the import and autoload from the config.
Could someone demonstrate how they've created a new model and CRUD and successfully accessed its actions?
Thanks in advance
I have created a new model, InstTypes with the CRUD. Why does
http://localhost:8081/we#ss/instTypes/create return Not Found (#404)?
You cannot access a model directly, the only way to interact with a model is via a controller which will intern interact with the model and the view. By initializing the model $model = new YourModelNmae(); or by rendering a view.
From your URL
http://localhost:8081/we#ss/instTypes/create
InstType should be your controller while create is an action under InstType
Using Yii2 Gii tool to generate a CRUD do the following : -
Generate your model
Generate your CRUD
go to
http://localhost:8081/we#ss/yourController/YourActionOnYourController
Refer to this youtube link for more detail https://www.youtube.com/watch?v=6B52-li6IgU
Happy coding :)
Just to add to the other answers, your url isn't working because it's camelCase. You need to make it hyphenated, so
http://localhost:8081/we#ss/instTypes/create
will become
http://localhost:8081/we#ss/inst-types/create
You're getting the 404 because you're trying to access instTypes, it should be inst-types.
this solved my problem. I needed to add the controllers to the controller map.

Yii2 Load Controller

I created one controller SlideController for manage the slide on my website.
But In the SiteController I just want to load some action in the SlideController.
Is it possible or not? If it's possible ,how can I?
Yes, it is possible.
The proper way I think, is to create yii2\base\Action subclasses which contain common actions shared by different controller.
As a reference on how to write Action and how controller links up it, you can read the source code here - https://github.com/yiisoft/yii2/tree/master/framework/rest
You can use this for your need:
https://stackoverflow.com/a/30432980/748156
In your case, in SiteController, set it like:
Yii::$app->runAction('slide/action', ['param1'=>'value1', 'param2'=>'value2']);