How to Create MY_Controller in Yii2 - yii

I'm newbie in Yii, especially Yii2. How can I create MY_Controller like CI in YII2 ? so other controllers will extend to MY_Controller.

in YII2, called as BaseController. I think in another framework have same name BaseController .
First, if you're using basic template, create BaseController.php inside component directory.
namespace app\components;
use Yii;
use yii\web\Controller;
use yii\helpers\Url;
class BaseController extends Controller
{
public function init()
{
parent::init();
}
public function _anotherMethod(){ /* your code goes here */ }
}
Next in your other controllers :
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\components\BaseController;
class YourController extends BaseController
{
public function init()
{
parent::init();
}
public function _anotherAction()
{
// your code
}
}
I hope it will help you

Related

Model not found in Laravel 5.6

My route in api.php is like below
Route::apiResource('/suras', 'SuraController');
My Model Sura.php is like below
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sura extends Model
{
}
My Controller SuraController.php is like below
<?php
namespace App\Http\Controllers;
use App\Model\Sura;
use Illuminate\Http\Request;
class SuraController extends Controller
{
public function index()
{
return Sura::all();
}
}
I am trying to browse in below URL
http://127.0.0.1:8000/api/suras
I am getting below error
It's looking for the class:
App\Model\Sura
But you declare your namespace as:
namespace App; // Which gives App\Sura
Therefore, just change the namespace of your class to:
namespace App\Model;
And move the class into app/Model directory.

Symfony 3.3.10 routes

I have problem when i try to use routes. It can't be generated, i tried via /app/config/routes.xml but when i modify i get error that the file is not YAML format.
The controller looks:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
class MainController extends Controller
{
public function indexAction(Request $request)
{
return $this->render('main/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
}
When i try to visit /index or main/index it gives me route not found!
:#
Nor works when i put use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; first before Controller.
First, you have to put namespace on header.
Second in Symfony 3.3.10 which route you want to use have to declare before on public function indexAction.
So, your route declaration would be as follows:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
class MainController extends Controller
{
/**
*#Route("visit/index")
*/
public function indexAction(Request $request)
{
return $this->render('main/index.html.twig', ['base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,]);
}
}

Register an asset in Yii2 for all views in a module?

I have a module in Yii2 containing a lot of controllers, models and views.
How can I register an asset for all views, without register it in all view one by one?
The module has init() method, you can use it for code that needs to be executed every time the module is accessed:
<?php
namespace frontend\modules\users;
use frontend\assets\UsersAsset;
use Yii;
use yii\base\Module as BaseModule;
class Module extends BaseModule
{
/**
* #inheritdoc
*/
public $controllerNamespace = 'frontend\modules\users\controllers';
/**
* #inheritdoc
*/
public function init()
{
UsersAsset::register(Yii::$app->view);
parent::init();
}
}
Don't forget to call parent implementation.

User scope with Zend Framework

I'm switching a Zend Framework application from mono-user to multi-user.
What is the best approach to include the user scope in the controllers ?
One way would be to add the user id in each methods in every controllers:
/application/controllers/IndexController.php
...
public function indexAction() {
$params['user_id'] = Zend_Auth::getInstance()->getIdentity()->id;
$listHelper->readItems($params);
}
...
An other one would be to create a new User model and fetch his items :
/application/controllers/IndexController.php
...
public function indexAction() {
$userModel = new application_models_user();
$userModel->find(Zend_Auth::getInstance()->getIdentity()->id);
$userModel->readItems();
}
...
I'm wondering what's the best approach that would allow me to write minimal code and if you have another idea to "automagically" add the user scope (db scope, plugin...).
Create an abstract class by extending Zend_Controller_Action
abstract class My_Controller_Action extends Zend_Controller_Action {
private $userModel;
public function getUserModel() {
if(is_null($this->userModel)) $this->userModel = new application_models_user();
return $this->userModel;
}
public function getUserId() {
return $this->getUserModel()->find(Zend_Auth::getInstance()->getIdentity()->id);
}
}
Now use this class as base class for your controllers.

yii - variable available to each controller

I am new to yii.
I am using more than 1 controller in my website and each controller has few actions.
I want to use some variables across each controller (Value of variable will be fixed, I need some constants for a formula). Whats the best place (standard way) to define those variables ?
Should I use session ? (as value is not going to change).
Not sure what you are using your vars for, but you can do it by defining them in your config main.php
'params'=>array(
'someVar1'=>'varValue1',
'someVar2' => 'varValue2',
),
Then you can access them in ANYWHERE by calling
Yii::app()->params['someVar1']
They will be available anywhere in your application.
Or you can extend all your controllers off of a base class and define your constants there
Base Controller:
class Controller extends CController {
const SOME_VAR = 'someValue';
}
Your controller:
class YourController1 extends Controller
{
public function actionIndex()
{
echo parent::SOME_VAR;
}
}
Your other controller:
class YourController2 extends Controller
{
public function actionLogin()
{
echo parent::SOME_VAR;
}
}