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.
Related
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.
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]);
}
Hi I have a file called payment.php it contains some functions related to payments and some multiple classess. So I want to include that file in my symfony2 Controller to access its all methods and classless. I am trying it as follows:
//File location is in src/AppBundle/Controller/payment.php
namespace AppBundle\Controller;
require_once __DIR__.'/./payment.php';
//My controller
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
But I am not able to access the file and its methods.
I am using this approach because keeping it in /vendor directory it also not able to access because this file contains multiple classless in same files.
Please advice me how can I access this file in my controller.
thanks in advance
If paymant.php have classes you need to make instance of that class to call method from it, that's like basic OOP stuff.
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
First of all, where is the method in this controller where you want to call your method? Then, why you are calling payment on $this if it comes from diffrent class. It should be something like this
class ApiServicesController extends Controller
{
public function indexAction()
{
$somePaymentClass = new SomePaymantClass(); //whatever class you want from payment.php
$somePaymentClass->payment(array('txnId'=>1112548));
}
}
But iI strongly recommend to use it as a service and put it in some autoloader namespace.
You have to make a Payment class as a service and then you can use all functions of Payment class in controller. Please refer this document.
http://symfony.com/doc/current/service_container.html
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.
I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?
check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}
Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};