I am setting session variable in function of a controller like below.
use Illuminate\Support\Facades\Session;
class UserController extends Controller
{
public function store(Request $request)
{
session(['user_name' => $user_name]);
}
}
I am trying to access that session variable in another function of another controller.
use Illuminate\Support\Facades\Session;
class DashboardController extends Controller
{
public function __construct()
{
dd(session('user_name')); // I am not getting value here
}
}
I am not getting value from Session Variable.
You can do it like this
use Illuminate\Support\Facades\Session;
class UserController extends Controller
{
public function store(Request $request)
{
session()->put('user_name', $user_name);
}
}
And you can get it another controller or anywhere like this
session()->get('user_name');
Hope this will help you, thanks..
Related
In my controller(s), instead of fetchingAuth::id() in each method, I've set up an $id property in the controller's class and fetched it once in the constructor. then, in the rest of the methods i'm just refering $this->id, is it considered safe or am I doing something wrong?
Code Sample: http://pastebin.com/pvju54eh
What you could do is inject the Guard instance in your controller and then assign the currently logged in user (if there is one) to a class property:
<?php namespace App\Http\Controllers;
use Illuminate\Contracts\Auth\Guard;
class SomeController extends Controller
{
protected $auth;
protected $user;
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->user = $this->auth->user();
}
public function someMethod()
{
// Get logged in user’s ID
$userId = $this->user->id;
}
}
I do not see any major problems with this approach even though I have not seen approach used often.
Myself I find it easier to get the $request->user() in controller from Request though.
I have the Module_One in which function exampleOne()...
class Model_One extends CI_Model {
function __construct()
{
parent::__construct();
}
function exampleOne(){
return "test";
}
}
I wonder how, (if that is possible), call the Model_One within Model_Two...
class Model_Two extends CI_Model {
function __construct()
{
parent::__construct();
}
function exampleTwo(){
return "testTwo";
}
}
In a way that I could just call the Model_Two and use the exampleOne() of Model_One.
class Controller_One extends CI_Controller{
function index(){
$this->load->model('Model_Two');
$this->Model_Two->exampleOne();
}
}
I know I could do it that way ...
class Model_One extends CI_Model {
function __construct()
{
parent::__construct();
}
function exampleOne(){
return "test";
}
}
and so ...
class Model_Two extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->model('Model_One');
}
function exampleTwo(){
return "testTwo";
}
function exampleOne2(){
$this->Model_One->exampleOne();
}
}
and so ...
class Controller_One extends CI_Controller{
function index(){
$this->load->model('Model_Two');
$this->Model_Two->exampleOne2();
}
}
However, redundant create a function to call another function, I know it must have another way to do this, but I do not know, and found nothing upon. anyone any ideas?
Thanks for your attention
yeah, Programming should DRY (Dont Repeat Yourself).
for your case, try this code :
class Model_Two extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->model('Model_One');
}
function exampleTwo(){
return "testTwo";
}
// change this method to the name of the another module
function model_one(){
// just return the new model object
// so you can keep using all method in this object, without repeat the methods
return $this->Model_One;
}
}
and in your controller, you use the model_two model like this :
class Controller_One extends CI_Controller{
function index(){
$this->load->model('Model_Two');
$this->Model_Two->model_one()->exampleOne();
// and here you can use all method from model_one...
// $this->Model_Two->model_one()->exampleTwo();
}
}
I hope this help for you. :)
I would to override constroller constrcuter's like this :
class XControler extends AppController {
public $attr = null;
public __construct(){
$this->attr = new YController();
}
}
But when I do that I take error ! can you explain me why and how I do that with out using requestAction just OOP !
thanks
Controllers are responsible for dealing with end user requests. Each controller action should have a view, and normally you would not want to access the methods from YController inside XController.
What you want to achieve can be done this way:
XController.php
App::uses('YController', 'Controller');
class XController extends AppController {
public $attr;
public $uses = array('Person');
public function __construct($request = null, $response = null) {
$this->attr = new YController();
parent::__construct($request, $response);
}
public function method1() {
// you can now call methods from YController:
$this->attr->Ymethod1();
}
}
YController.php
class YController extends AppController {
public function Ymethod1() {
// ....
}
}
However, the business logic should be inside Models or Components. This is the proper way to share methods between more controllers.
So your XController should look like:
class XController extends AppController {
public $uses = array('Model1');
public function action1() {
$this->Model1->method1();
// ....
}
}
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.
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;
}
}