Laravel how to include a partial in the base controller - php

In my laravel application I am using a Base_Controller class and then extend this class in other controller.
In my app there is a variable which I need to use in all my controller and templates.
This is why I tried to use in my base controller.
$this->layout->myVar = 'stuff'
But when I try use $myVar in my view I am getting an error:
Creating default object from empty value
My base class constructor is something like this:
public function __constructor()
{
parent::__construct();
$this->layout->menu = 'stuff';
}
Does anyone have any idea on what is the best way to approach this?

You must have $this->layout defined in your controller class or else $this->layout must be assigned an instance of View::make().
$this->layout->menu may also require having the #section('menu') #endsection in it.

Related

Laravel , how to call a function from another controller

I have a controller with the "getUsers" function in a controller called "UserController" , and inside it I want to call a function of the "CarController" controller called "getCars", the two options I have are:
a) Make the second call as "static" , then I can call it without instantiating the class
b) Do not do that function of the static class and I call it in this way
$ car_id = 100;
$ userController = new UserController ();
$ userController-> getCars ($ car_id);
I do not know which is the best practice, or what pros or cons has one or another.
I'm using laravel.
Thanxs.
It is a bad practice to call a controller from another controller, this usually signals that you have badly designed your code and you should think of a different way to achieve what you want.
None the less, you can do it like this:
app()->call('App\Http\Controllers\CarController#getCars');
If your controller method has parameters you can pass them as the second argument:
app()->call('App\Http\Controllers\CarController#getCars', [$param1, $param2]);
To answer your question, you should not call one controller method from another. As #elfu mentioned, this is not the intended functionality of a controller anyway. His post is correct and in your case you should probably use the User model as the location of this method, but I thought I'd at to it a little.
If you do want to share methods between multiple controllers, a good place to do this is through a Trait. In some cases, you are not referencing a model that is shared between controllers, and a Trait would be your best option.
To include a trait, you can reference it by including it at the top of your controller and then with a 'use' statement after the class declaration for the controller. Here is an example:
use App\Traits\ExampleTrait;
class CarController extends Controller
{
use ExampleTrait;
...
You would do the same in the UserController. Then, any method that you place in the ExampleTrait will be directly accessible from the CarController and the UserController by referencing it as $this->methodName(), just like referencing any other method in the same controller.
In your particular case, I would say that your logic should probably be stored in the User model, since the cars for a user are really an ATTRIBUTE of the User model, but the above gives you another option to work with.
In my humble opinion you should not call another controller in a controller.
It looks like you have some business logic in that controller. So you should move your logic to the entity (User.php) and call it in both controllers methods.
A regular controller returns a view (at least that is what is expected), so if you want to call another controller you should just send that route to that method (in web.php file) instead of calling it in another controller.
Hope that helps you.
You can call one controller function from another but the best way is to create a trait and use it both the controllers like:
trait Common
{
public function method(){}
}
class FirstController extends Controller
{
use Common;
}
class SecondController extends Controller
{
use Common;
}
If you want to bind parameters to the call, you can use:
$videos = app()->call('App\Http\Controllers\StorageController#returnViewVideo',[
'course'=>$course,
'lesson'=>$lesson,
]);
The following code worked for me well. and also it also can be used in routes.php
public function mobileImageUpload(Request $request){
$this->validate($request,[
'data'=>'required',
'filetype'=>'required',
'userid'=>'required',
]);
$namespace = 'App\Http\Controllers';
$controller = app()->make($namespace.'\ImageController');
return $controller->callAction('mobileImageUpload',[$request]);
}

Global Query Data

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}
Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.
Declare this function as protected in parent controller class.
No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

How to run code before all my methods inside controller in Laravel 4?

I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views

how can I store $data array for all methods in a controller

I have some data array which I need for all method's in a controller.
$data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
Problem is I cant DRY this. so I have paste this $data array in each method.
I have a view page for this. so when I load this view , I have to
load above $data array each time/method. this is disgusting when controller
methods are too much.
I want 1 piece of this code like constructor. How can I do this.
you can use traits for this.
Define your methods in a trait, and then use the trait in the controllers.
You can make one helper class in which make a function and put your above code in it but make sure you can't access model using $this so, you need to create CI instance and than access it. after that in your controller in construct method you just need to call this function but don't forget to load the helper class and store it in a variable and pass it along with view.
Just create private data variable in your controller class. Than set your data in constructor. Now you can access your data in any method you want.
class Pages extends CI_Controller {
// ...
private $data;
// ...
public function __construct() {
parent::_construct();
$this->data = array();
$this->data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$this->data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$this->data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$this->data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$this->data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$this->data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
}
// ...
}

$this keyword in view in CodeIgniter

I'm trying to understand how $this->load->view() works inside of a view file in CodeIgniter.
The core/Controller.php is calling core/Loader.php which then calls _ci_load() which in turn does an include('/path/to/view');
Shouldn't $this refer to the Loader class at that point? How is $this referring to the controller?
By my understanding, you should have to call $this->view() inside of a view file. Not $this->load->view() because the load() function is not accessible inside of the Loader. It's a class variable of the Controller base class. i.e, $this->load =& load_class('Loader');
Please note: I'm trying to understand the CodeIgniter internals. I know perfectly well how to embed view files in other view files as a CodeIgniter user. Please do not leave answers explaining how to use $this->load().
To simplify the understanding of what $this refers to in a view, since a view is "loaded" by a controller method, the view is still run in the same scope as that method, meaning $this can have a different context depending on which class loaded it.
For example:
class Controller1 extends CI_Controller {}
In any view file loaded in this example controller, $this refers specifically to the Controller1 class, which can access CI_Controller public and protected properties/methods as well (like the Loader or Input classes, which are assigned to the load and input properties of CI_Controller) since it extends that class.
Controllers are still just plain old PHP classes. If I were to do this:
class Controller1 extends CI_Controller {
$this->foobar = 'Hello';
}
class Controller2 extends CI_Controller {
$this->foobar = 'World';
}
...if we load the same view file in any method of either of these controllers, using $this->foobar in that view file will return a different value.
Last time I checked, $this was of class CI_Loader, try var_dump($this); inside a view.
Check out:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Controller.php
is_loaded(); returns an array with the already loaded classnames and their aliases from the main container.
$this->load is then an instance of CI_Loader inside the controller.
Check:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php
Line 778

Categories