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();
}
Related
I'm trying to redirect to an external URL from a helper class.
Here's my controller:
<?php
namespace App\Http\Controllers;
use App\Lead;
use Illuminate\Http\Request;
use App;
use Helper;
class MyController extends Controller
{
public function entities_get() {
Helper::my_function(); // <---- Call my Helper class method to redirect.
return view( 'template' );
}
}
Here's my helper class used in the controller:
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away( 'https://www.google.com' ); // <---- Not redirecting.
}
}
Why is the redirect() not working inside my_function()? Do I need to include some Laravel classes using the PHP "use" statement?
You can add send method and it will work.
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away('https://www.google.com')->send();
}
}
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
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);
}
}
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);
}
}
I have a problem with passing variable (data) to all Views. I created BaseController that extends default Laravel controller and "global" variables are defined there. When I extend other controller with BaseController i got error that variable is not defined. Does someone knows where's the problem?
Here is code:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class BaseController extends Controller {
public function __construct() {
$obavijesti="Other data";
$izbornici="Some data";
View::share ( 'izbornici', $izbornici );
View::share ( 'obavjesti', $obavjesti );
}
}
class AdminController extends BaseController {
.
.
.
echo '<pre>';var_dump($izbornici);echo '</pre>';//Error pop ups here
.
.
.
}
You are doing something wrong here. view::share() is used for sharing a piece of data across all views not controller.
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
//If you wish to get these variables in your other controllers you do this:
class BaseController extends Controller {
public $obavijesti="Other data";
public $izbornici="Some data";
public function __construct() {
View::share ( 'izbornici', $this->izbornici );
View::share ( 'obavjesti', $this->obavjesti );
}
}
class AdminController extends BaseController {
//if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:
public function __construct(){
parent::__construct();
}
public function Index(){
echo $this->obavijesti;
}
}
You can also use a composer to share variables to views
//1. Create a composer file at app\Composers\AdminComposer.php
//NB: create "app\Composers" if does not exists
//2. Inside AdminComposer.php add this.
<?php namespace App\Composers;
class AdminComposer
{
public function __construct()
{
}
public function compose($view)
{
//Add your variables
$view->with('izbornici', 'Other data')
->with('obavjesti', 'Some other data');
}
}
//3. In you controller do this:
<?php namespace App\Http\Controllers;
//NB: I removed your BaseController because I believe the issue is coming from //there
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class AdminController extends Controller{
public function __construct(){
//Lets use AdminComposer to share variables to adminpage.blade.php view
View::composers([
'App\Composers\AdminComposer' => array('adminpage')
]);
}
public function Index(){
return view('adminpage');
}
}
Ideally you're going to want a combination of what you have, and the other answer posted here.
<?php
class BaseController extends Controller {
protected $obavijesti = 'Other data';
protected $izbornici = 'Some data';
public function __construct() {
View::share('obavjesti', $this->obavjesti);
View::share('izbornici', $this->izbornici);
}
}
Then in all of your views, you have access to the variables $obavjesti and $izbornici. Now in your other controllers, anything that extends BaseController can do the following:
class AdminController extends BaseController {
public function index() {
echo $this->ixbornici;
echo $this->obavjesti;
}
}