Home | About | View All Posts

6 Jul 2016

YII2 Development - Interview Questions and Answers

Yii ias fast, secure and high-performance component based PHPframework for developing large-scale Web applications rapidly. Below are the list of frequently asksed YII2 developement questions and answers.


Q. - What is the path alias in yii2?
Ans. - Yii provides a set of aliases to easily reference commonly used file paths and URLs. We can define or create custom aliases in Yii2.

Below are the list of predefined path aliases list in Yii2 -
  • @app: Your application root directory (either frontend or backend or console depending on where you access it from)
  • @vendor: Your vendor directory on your root app install directory
  • @runtime: Your application files runtime/cache storage folder
  • @web: Your application base url path
  • @webroot: Your application web root eg. - "C:/wamp64/www/yii2cms/backend/web"
  • @common: Alias for your common root folder on your root app install directory
  • @frontend: Alias for your frontend root folder on your root app install directory
  • @backend: Alias for your backend root folder on your root app install directory
  • @console: Alias for your console root folder on your root app install directory
  • @yii, the directory where the BaseYii.php file is located (also called the framework directory).
  • @bower, the root directory that contains bower packages. Defaults to @vendor/bower.
  • @npm, the root directory that contains npm packages. Defaults to @vendor/npm.
You can get path alias path using the codes at any view file like = (echo Yii::getalias('@app');, echo Yii::getalias('@web');

Custom definition for path alias -
We can define an alias for a file path or URL by calling Yii::setAlias(). For examples -
// an alias of a file path
Yii::setAlias('@foo', '/path/to/foo');
// an alias of a URL
Yii::setAlias('@bar', 'http://www.example.com');
// an alias of a concrete file that contains a \foo\Bar class
Yii::setAlias('@foo/Bar.php', '/definitely/not/foo/Bar.php');

Q. - What do you know about route in Yii2?
Ans. -
Yii routes refers to module, controller and the action requested :
ModuleID/ControllerID/ActionID

Here, the ModuleID is optional, so generally the format is ControllerID/ActionID

Example route :
http://hostname/index.php?r=site/index, where site is the ControllerID and index is the ActionID.

Here site points to app\controllers\SiteController.

Q. - How to run sql in controller file and passing the array into view file?
Ans. - We can call and run sql query at controller file also for testing. Below are the controller and view file code :
namespace app\controllers;
use Yii;
use yii\web\Controller;
class YourController extends Controller
{
public function actionIndex()
{
$sql = 'SELECT * FROM tablename ORDER BY id ASC';
$db = Yii::$app->db;
$output = $db->createCommand($sql)->queryAll();
// same of
// $output = Yii::$app->db->createCommand($sql)->queryAll();
return $this->render('index', [ 'output' => $output ]);
}
}

View File code -
foreach($outputs as $item) {
echo $item['column1'] 
echo $item['column2']
---------
-------
------


Q. - What parameter is entered into gii crud generator?
Ans. - Below are the parameters entry for creating crud generator for a exiting class - "Departments" for yii advanced template in 'backend'.

Model Class :(Model must exist before start of the crud. Please generate the "Model" for Departments, before crud start.)
backend\models\Departments

Search Model Class :
backend\models\DepartmentsSearch

Controller Class:
backend\controllers\DepartmentsController

View Path: you can use yii path alias for this. You can get path alias path using the codes at any view file like = (echo Yii::getalias('@app');, echo Yii::getalias('@web');. @app will produce - H:\projectname\backend, @web will produce - /projectname/backend/web
@app/views/departments
Best way - you should use physical path.
eg. - /wamp64/www/yii2cms/backend/web/themes/yourthemename/views/departments
Below will also work -
@webroot/themes/yourtheme/views/yourviewsname

Base Controller Class:
yii\web\Controller

Widget Used in Index Page:
GridView

Tags :

0 comments:

Post a Comment