CodeIgniter: Not able to hit controller specific method - php

I am new to Codeigniter. While trying to access controller specific method, I am getting 'Object Not found' exception.
Before hitting the url I did following changes:
base_url set as http://localhost/test/ in config.php
default_controller set as main
Defined index and login method in main.php class
If I try to hit just http://localhost/test/, it returns echo from the index method. But if I directly give http://localhost/test/main/login then it throws Object not found exception. Strangely if I give $this->login(); into index method of main controller class, http://localhost/test/hits the login method. I have tried to change login method to public but no luck.
What I am missing here?

It seems to be the common index.php problem
This should do it: http://ellislab.com/codeigniter/user-guide/general/urls.html
But please confirm it by trying to access the controller with a index.php in behind it like so:
http://localhost/test/index.php/main/login

Related

Codeigniter Custom Controller Receiving 404

So I copied the controller file Welcome.php and named it home.php.
I changed the class name from Welcome to Home.
When I go to view it in http://daytodata.net/ci/index.php/home I get a 404?
Did you try accessing your method using /home/index/ ?
Also you should consider take advantage from routing system:
https://www.codeigniter.com/userguide3/general/routing.html

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

Codeigniter: call a controller from another codeigniter file

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");

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")

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