vendredi 30 septembre 2016

Single Inheritance of Controller Classes in Laravel 5.2

Hi i am trying to do single inheritance in Laravel 5.2 for Controllers.

This is my parent basecontroller class

 <?php

 namespace App\Http\Controllers\Moviesfrontend;

 use Illuminate\Http\Request;

 use App\Http\Requests;
 use App\Http\Controllers\Controller;


 class BaseController extends Controller
 {
     public function __construct($request)
     {
         //code to check whether city is set in session or not
         $selectedcity=$request->session()->get('selectedcity');

     }
 }

In BaseController class construct will fetch the city name stored in session. Now i want to access this city name stored in $selectedcity in my child class.

My child controller class is

  <?php

  namespace App\Http\Controllers\Moviesfrontend;

  use Illuminate\Http\Request;

  use App\Http\Requests;
  use App\Http\Controllers\Controller;

  class HomeController extends BaseController
  {

      public function __construct(Request $request)
      {

          parent::__construct($request);

      }

      public function index(Request $request)
      {
          echo $selectedcity;
      }
  }

Bedefault I guess parent class variables are accessible to child class but this is not working and i am getting error message that $selectedcity not found.

Kindly assist me accessing this variable in chlid class.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire