Session.php class is not able to load properly - php

I am working on PHP in Code-igniter. I want to call function of one controller on another controller. I have tried following code for that-
$this->load->library('../controllers/Benchmarking');
$this->Benchmarking->init_benchmark();
But after doing this I got an error as unable to locate the specified class:session.php
(Here, benchmarking is my controller and init_benchmark is that function which I want to call)
Can anyone help me, please?

You cannot call a function of a controller from another controller. CI won't allow it.
If you need the user to "move" from a controller to another, your best bet is to set session flashdata with all the data you need to pass from one controller to the other and then use a simple redirect.
$this->session->set_flashdata('handoff_data');
redirect('second_controller/function');
There are other ways, some more powerful, some more complicated, but this is by far the simplest one

please check your class name and extends properly. and create a common controller in your core folder like
class MY_Controller extends CI_Controller {
}
and call this controller in another controller's extends.

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

Symfony: How to force a password reset for a user after login/during an active session

I have a User entity with a resetPassword property on it. It's boolean, if true the user should be redirected to a resetPassword form.
At the moment I have this logic in my DefaultController. When someone hits the index route the controller action checks for the flag and renders a resetPasswordType form when needed. It's OK but far from ideal: without cut/pasting code it won't apply to all routes.
What is the best practice to properly implement this?
I've had a few ideas:
An event listener like the one described here might do the trick. I have tried it, and I managed to get the user token and got the check to work but I'm not sure what the best way to render the formType is from there. Maybe I could just redirect to a route from the event listener, but again I'm not 100% sure on how to do this.
A custom userChecker. This would only work after login but that's OK in this case. Again, I have tried it, it worked, but I'm not sure on how to render the form from there.
Some sort of custom redirect after success but I'm not sure on where to actually do the check on the resetPassword flag in this case.
Any opinions/advice welcome.
Create MY_Controller.php in application/core:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
//do your works...
}
}
Then , extends all your controllers :
class your_controller extends MY_Controller {}
Your code will run befor any controller (extented from MY_Controller ) run.

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]);
}

Possible to change action class within Yii2?

Is it possible to change the action class Yii2 uses somehow, similar to how you can set the class of many other components within the config file?
I want to extend this class so I can add another member variable to it.
I guess I could just add one to it anyway dynamically, but would prefer to do it in a proper fashion.
Edit: Looking at the list of core application components it isn't listed, so not sure if it's possible?
The proper way to solve this problem is to extend both controller and action classes. If you look at the source code, yii\base\Controller has a createAction method that, if no class action is found, will create an instance of InlineAction.
Since you're extending some kind of controller class every time you make your own controller (class MyController extends Controller), you can just override the original createAction method and in it use your own implementation of the InlineAction class.
It can be done with class map
Yii::$classMap['yii\base\InlineAction'] = '#common/InlineAction.php';
and should be placed into index.php, before the app is launched.
Regardless of its location, common/InlineAction.php should have the same yii\base namespace as the original class.

Would bypass of the controller in CodeIgniter be considered a good practice?

Is directly call to the Model class inside the View is best practice or not? Currently I am using CodeIgniter to develop an application. In different Views of my application I'm including menus that I want to pull from the database. And the thing is currently I am passing the values to the menu through the controller. If I make a common model class and call it from the View and by pass controller. So that there will be one call to Model and it will load menu from the database at once and by pass the controller. By doing this what pros and cons will come?
With codeigniter, your views should not be concerned as to where data comes from, only that it exists. Only your Controllers should be in direct contact with your Models.
It sounds like you have a common menu that you want to load in your views and you don't want to replicate that code across all your Controllers.
To solve this problem, you need to create a common controller that your primary controllers inherit from with a method that fetches the menu.
My_Controller needs to be saved to the core folder in the application directory.
class MY_Controller extends CI_Controller
{
protected function get_menu()
{
// Load your menu here
$this->load->model('menu_model');
return $this->menu_model->get_menu();
}
}
All your primary controllers will inherit MY_Controller
class Home_Controller extends MY_Controller
{
public function index()
{
$page_data = array('menu' => $this->get_menu());
$this->load->view('home/index', $page_data);
}
}
In my opinion no its not best practice. View should contact Controller and Controllers should retrieve data from Model where Model does all the logic. Controller suppose to be the glue or middleman of View and Model.
Controller passes the returned data into View and then you do your foreach loop or whatever to display it.
View should not be doing any logic. Retrieving data from database is somewhat logic.
It is a worst practise. Then why we have controller concept for MVC. :) Mainly all logics goes into Model so thats not good to call model directly.
There will not be any problem, since you are using a common controller.
Note : You should not call model directly from view.

Categories