laravel parent constructor does not called child constructor - php

I am using laravel 5.2,
I have created admin controller and added logic to check admin role in constructor
namespace App\Http\Controllers;
use Sentinel;
class AdminController extends Controller
{
public function __construct()
{
if(Sentinel::check())
{
if(!Sentinel::inRole('admin'))
{
return redirect("login");
}
}
else
{
return redirect("login");
}
}
}
and I extends this controller on some admin controller
namespace App\Http\Controllers;
use Request;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use Sentinel;
use App\User;
use DB;
class UserController extends AdminController
{
function __construct()
{
parent::__construct();
}
}
When I call user controller admin constructor is called but return function is not working properly, if I add die; before return it get die, but after return notthing is affected.
so it doesn't return redirect function properly.

The ugly workaround would be to pass a boolean param to Papa indicating that you do not wish to parse the code contained in it's constructor. i.e:
// main class that everything inherits
class Grandpa extends Controller
{
public function __construct()
{
}
}
class Papa extends Grandpa
{
public function __construct($bypass = false)
{
// only perform actions inside if not bypassing
if (!$bypass) {
}
// call Grandpa's constructor
parent::__construct();
}
}
class Kiddo extends Papa
{
public function __construct()
{
$bypassPapa = true;
parent::__construct($bypassPapa);
}
}

Related

How to delete parent static methods in extended class

Here is my model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Associate extends Model
{
// some code
}
In controller I use this model similar this
<?php
namespace App\Http\Controllers;
use App\Models\Associate;
use Illuminate\Http\Request;
class AssociatesController extends Controller
{
protected $associate;
public function __construct(Associate $associate)
{
$this->associate = $associate;
}
public function edit(Request $request, $id)
{
$associate = $this->associate->with('some-relation')->find($id);
// other part of code
}
}
When i wont to testing in controller edit method using phpunit I cant mock with method because it is static method of Illuminate\Database\Eloquent\Model.
My question there is way to delete some method of parent class??
From Laravels documentation
static Builder|Model with(array|string $relations)
Being querying a model with eager loading.
From Php docs
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
?>
The above example will output:
B

Class App\Http\Controllers\Panel does not exist

I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}

Why I didnt see the result in my extended constructor in Laravel

I have few classes which extends from the abstract class
And Class MenuController Extends from SiteAdminController
I need to call MenuController and receive authenticated user id
<?php
namespace App\Http\Controllers\SiteAdmin;
use App\Http\Categories;
use Illuminate\Http\Request;
use Gate;
use App\Category;
use App\Http\Controllers\MenuController;
use App\Site_categories;
use Auth;
class SiteAdminController extends \App\Http\SiteEntity implements Categories
{
protected $host;
public $user;
public function __construct()
{
parent::__construct();
$this->middleware('auth:admin');
}
protected function menu() {
return $data_nav['menu'] = MenuController::index('admin_categories');
}
Other one extends from SiteAdminCntroller
<?php
namespace App\Http\Controllers\SiteAdmin;
use Illuminate\Http\Request;
use Gate;
use Auth;
use App\Category;
class MenuController extends SiteAdminController
{
public $category_menu;
public $user_categories;
public $user;
public function __construct(Auth $auth)
{
//parent::__construct();
$this->user_categories=$this->CategoriesMenu();
$this->user=$auth::guard('admin')->user()->id;
dd($this->user);
//dd($this->user_categories);
}
I think the constructor in the MenuController run befor the middlware in SiteAdminController
Thats why I have such error
http://prntscr.com/hwfifx
Please Explaine what have I do to see result from me dd() function?
I was trying even to call parent::__construct but it not helping
You are correct that the the code in the constructor runs before the middleware: https://github.com/laravel/framework/issues/15072
The easiest way to get around this is to use the middleware method in the controller:
MenuController
public function __construct()
{
parent::__construct();
$this->middleware(function () {
$this->user_categories = $this->CategoriesMenu();
$this->user = auth()->guard('admin')->user()->id;
});
}
First of all check if the class see another class that must be extended with.
Then try below approach (it s just example):
class ConceptController extends \SiteAdminController {
public function __construct(SiteAdminController $siteAdmin) {
parent::__construct($siteAdmin);
}
}

PHP/Laravel parent class variable is null when calling from different controllers

Parent Class:
<?php
namespace App\Services;
class RequestVariables {
protected static $keys_tour;
public static function init() {
self::$keys_tour = array_flip(['tour_type', 'city_from']);
}
}
Child Class:
<?php
namespace App\Services;
class PreviousVersions extends RequestVariables {
public static function createVersion ($tour) {
dd(parent::$keys_tour);
}
}
When I call PreviousVersions::createVersion() from 1st controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\PreviousVersions;
use App\Tour;
class Tours2Controller extends Controller
{
public static function PreProcess($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs what's expected:
array:2 [
"tour_type" => 0
"city_from" => 1 ]
but when I execute the same function in another controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tour;
use App\Services\PreviousVersions;
class BookingController extends Controller {
public function booking($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs 'null'
I can't see what's different between my controllers causing different results when calling the same method. Can somebody tell me why it outputs 'null' in the 2nd case?
If you need more information, please ask.
The $keys_tour property is being set inside the init() method of the RequestVariables class.
You can solve it by calling RequestVariables::init() inside the createVersions() method:
public static function createVersion ($tour)
{
RequestVariables::init();
}
Or using the parent keyword:
public static function createVersion ($tour)
{
parent::init();
}

How to 'copy' a Zend Framework Action without duplicating it?

I have two controllers which have some actions that are really the same.
How do I refer to the identical action in another controller?
class UserController extends Zend_Controller_Action {
public function listAction() {
//do something here
}
}
class AdminController extends Zend_Controller_Action {
public function listAction() {
//how to call UserController::listAction here?
}
}
What do I put in AdminController::listAction above so that I only have to write the code in UserController::listAction?
thanks
I would use a controller action helper, that way if you ever have to do the same thing again you can reuse it.
class My_Controller_Action_Helper_Whatever
{
public function direct()
{
return $this;
}
public function doSomething($paramA, $paramB)
{
// code
return $whatever;
}
}
Then implement in your controllers:
class UserController extends Zend_Controller_Action
{
public function someAction()
{
$this->getHelper('Whatever')->doSomething($a, $b);
}
}
class AdminController extends Zend_Controller_Action
{
public function anotherAction()
{
$this->getHelper('Whatever')->doSomething($a, $b);
}
}
You could do:
class baseController extends Zend_Controller_Action {
// common controller actions
public function listAction() {
// do stuff
}
}
class AdminController extends baseController {
// admin controller specific actions
}
class UserController extends baseController {
// base controller specific actions
}
You could also forward the request to the other controller by using:
class AdminController extends Zend_Controller_Action {
public function listAction() {
$this->_forward('list','user');
}
}
or if you would prefer the URL to change:
class AdminController extends Zend_Controller_Action {
public function listAction() {
$this->_redirect('/user/list');
}
}
You can forward to another action - simply specify the action, controller, module and params.
Parameters default to values of the current request, i.e. if you're in the default module, the code below will redirect to the listAction of UserController in the default module.
class AdminController extends Zend_Controller_Action {
public function listAction() {
//call UserController::listAction
return $this->_forward('list', 'user');
}
}

Categories