Codeigniter: call a controller from another codeigniter file - php

I have this two file path:
1: /api/math/application/controllers/test.php
2: /learn/mathtek/application/controllers/Auth.php
I need to call the controller from path2(Auth) inside the controller of path1(test). Is this possible? if yes how?
Ive tried to used the redirect() function but didnt work.
Also i tried this:
require_once('/learn/mathtek/application/controllers/Auth.php');
$aObj = new a(); //create object
$aObj->custom_a(); //call function
but it still didnt work...help please ... newbie in codeigniter here

Thanks guys for the help. I ended up doing a direct post of the path using redirect function to make it work.
1: /api/math/application/controllers/test.php - codeigniter1
2: /learn/mathtek/application/controllers/Auth.php - codeigniter2
Examples:
If i want to call path(2) from path(1).
redirect('http://localhost/learn/mathtek/auth/signin'); --'signin' is a function inside 'auth' controller
thats the code inside the 'test' controller. This works in codeigniter even if its from different path of controller.
However this doesnt work for UNITY(WEBGL). Thats why i ended doing an echo and returning it back.

You can't call any other controller action/method for have output or return value in your controller action/method because it is out of rules of MVC.
But you can redirect from one controller action to another controller action and pass argument in another controller action like below:
redirect("CONTROLLER/ACTION/ARGUMENT1/ARGUMENT2");
Edit:
suppose you are in Test controller and in test_method() action of Test controller then you can put your business logic code in the method and got some output and now you want to call any other controller function(eg: Auth) for perform any other operation with that output then you can pass that output in a redirect function as below:
redirect("Auth/auth_method/ARGUMENT1/ARGUMENT2");

Related

In yii framework for php how do you know which controller function to call on a particular url hit?

I work in laravel but in that we define in routes.php which controller action is to call on perticular url for eg.
route::get("login", "loginController#getLogin");
here we defining to call getLogin Action of controller loginController.php when thi url is hit http://********.com/login
but Now I am working with yii and it seems messy because i had defined a Action in controller posting the data to the action from view but view gives error because it didn't know which controller function . it gives invalidRouteException I found it in error_log.
you can use UrlHelper
$url = Url::to(['post/view', 'id' => 100]);
In this case is called the controller post the action view with the id=100
see this doc for guide http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
http://www.yiiframework.com/doc-2.0/guide-helper-url.html

How to call a function of a controller from a view?

There is a controller :
use Phalcon\Mvc\Controller;
class ReferentielClientController extends Controller
{
public function indexAction(){
// moteur de template pour une View
$this->view->act_form = 'ReferentielClient/ajouterClient';
return $this->view->pick("client/listerClient");
}
public function ajouterClientAction(){
$this->view->action_form = 'ajouterClientExec';
return $this->view->pick("client/ajouterClient");
}
...
}
From a view I want to call the indexAction method of the controller ReferentielClientController. How to do that ?
Short answer: You can't.
You can't since indexAction is not a normal method, it's an action. Actions aren't called by anyone in the usual way that methods are, they are called with routes an url. Let me explain this a little bit further:
Using MVC, we have the following route using :
http://www.example.org/a/b/c
a is which we call module.
b is which we call controller.
And c is which we call action.
Index actions and default modules will not appear in the URL.
So, how to call functions from view? Better not to. Best way to proceed is to do everything in your controller and pass that info to the view. I.e.:
public function indexAction(){
$this->view->myInfo = $allMyInformationInOneObject;
}
And then, in the view (index.phtml):
<p>
<?php echo $this->myInfo ?>
</p>
Summary:
You don't need to call any controller function from the view, just give it that info from the controller.
Added info about redirection
OK, answering to your commentary. What you want is not to call that function but to redirect the browser to that action.
So, I'll suppose you have your route created in your router, if not, please, go to the Zend documentation about routing. It's highly recommended to create routes instead of using the URL itself.
I guess that you will not be able to use the normal redirecting using href on a button or such, so you will need to use the redirector helper. Since you are not in a controller, you are not able to use the helper by using:
$this->_helper->redirector->gotoUrl($url);
So you will need to instance the redirector like this:
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl($url);
Being this $url, the URL given by your router, as you can read in the link above.
This can be done but I strongly recommend not to redirect from the view but from the controller. It would be good that all the logic remains in the controller.
I hope this helps!
Ok I found it : I set the href to point to the controller : Annuler

Call another controller action using renderPartial

i have problem in calling action from different controller using renderPartial.
I have one controller 'SiteController'. In which i call action from another controller 'AbcController'.
$this->renderPartial('Abc/_jobList',array('value'=>$value));
But i get following error
SiteController cannot find the requested view "Abc/_jobList".
Even i use
$this->renderPartial('//Abc/_jobList',array('value'=>$value));
and i get same error.
How can i solve it??
I think you are trying to access different controller's view.
For that you can access that by
$this->renderPartial('application.views.abc._jobList',array('value'=>$value));
But if you want to call another controller action then You have to redirect to that action from your current action using
$this->redirect("controllername/functionname")

Laravel : Using controller in included view

I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

Categories