What is required to call a controller in Codeigniter? - php

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.

Related

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

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

Removing controller name and function namein URL in Codeigniter CMS

I am building a cms in codeigniter and i want to remove controller name and function name form the url and just print the alias.
I will be having two controllers one for static pages and other for blog posts with categories.
Please help, Suggestions reagrding modification of two controllers are also welcome.
You will need to override the default 404 controller in application/config/routes.php.
$route['404_override'] = 'content';
Any request that can't be mapped to a controller will be passed to the application/controllers/content.php controller
Your Content controller, or whatever you decide to call it, will parse the uri [$this->uri->segment(1)] and check for a matching reference in your CMS database.
If there is no match in the database, then you can look for a static view in the views folder and load it.
if(is_file(FCPATH.'views/'.$this->uri->segment(1).'.php')) {
$this->load->view($controller,$this->data);
}
If no static view is found, and there is no matching content in the db, call the show_404() function.
Using this method, you will keep the default CI functionality of uri mapping, so at any time, you can add controllers as you normally would and the app will perform like a vanilla CI install.

Codeigniter url path

This is the most simple way I can ask this question as I have not fully understood what's going on, or what I am not doing right.
I'm having trouble with the url.
http://localhost/index.php/user is the same as http://localhost/
but
http://localhost/index.php/user/something is not the same as http://localhost/something
How do I make http://localhost/something work?
Does it have to be http://localhost/user/something, how do I make that work?
You need to understand how CodeIgniter's URLs work.
An URL consists of some segments. http://localhost/index.php/user/something/thing In this example user, something and thing are segments of the URL.
Segments of the URL indicate which controller and which method of that controller will run. http://localhost/index.php/user/something/thing In this example the method something from user controller is called and thing is passed to that method as a parameter.
The first segment of the URL indicates the controller.
The second segment of the URL indicates the method of that controller.
The following segments are sent to that method as parameters.
But there are some defaults.
If your URL is http://localhost/index.php/something, you have something specified as the controller, but because you have not specified any method, the default method which is index is called. So the above URL is the same as http://localhost/index.php/something/index
If your URL is http://localhost/index.php/, you don't have any segments specified (no controller and no method). So the default controller which is specified in application\config\routes.php is the loaded controller. Which method of that controller will be called? Of course the index method.
--You can set the default controller by changing $route['default_controller'] = "site"; to what ever fits your application in application\config\routes.php's file.
If you want http://localhost/user/something to be the same as http://localhost/index.php/user/something, you have to create custom routes for your application. More info on that here.
http://localhost/something indicates that you are calling the index method of the Something controller class
http://localhost/user/something indicates that you are calling the something method in the User controller class.
Does that make sense?
In order to make http://localhost/something work, you need a controller called something with an index method. This would be the same as accessing http://localhost/something/index.
Alternatively, http://localhost/user/something implies that you have a user controller with a method called something.
Does that help at all?
To remove index.php from your URL you have to use the mod_rewrite method described here
Then to remove the controller name (user) from the url, you need to use routes
In your case, you would add $route['^(something|something_else|etc)(/:any)?$'] = "user/$0"; to your routes.php file

Making a controller for home page

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc

Categories