codeigniter controller index function call requires its name - php

I have a Controller named Categories and one function in it called index with 1 parameter $cat_id
so it looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class categories extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index($cat_id = null){
}
}
the problem comes when i call it from the browser... i use this:
http://www.mysite.dev/categories/12311323
but returns 404 error page
instead if i use
http://www.mysite.dev/categories/index/12313131
will work fine...
how can i make sure it wont need index in the URL for the index function?

The default CodeIgniter routing is /controller/function/argument. In order to specify an argument, you need to first specify the function. If you want to specify an argument without the function, you need to define a custom route. Adding this line to your route configuration file should do what you want.
$route['categories/(:num)'] = "categories/index/$1";

What you describe are the default (pre-configured) routes of CI. You can define your own to make the pattern you describe in your question work. See: URI Routing

Related

Called controller can't access anything

in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}

passing variable to default controller in codeigniter

i have one default controller in codeigniter
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
as you know we can call this function
sitename/index.php
or
sitename/
as i have url rewrite in .htaccess
now i want to call same index function with parameter passing like
sitename/seo-services
where seo-services will be paramater passing to index function to load seo-services page.
as default syntax of codeigniter is
example.com/class/function/ID
i want in this formet
example.com/class/ID
where i want to skip the function for default (index) function of class
how can i do this?
Thanks
as you can see i am trying this for seo purpose.
Parameters appended to your url are automatically converted to arguments to your method.
For example if you have a method called index index in your welcome controller, then http://www.example.com/index.php/welcome/index/[parametervalue] will automatically pass [parametervalue] to your method, assuming that you define your function to receive the parameter
class Welcome extends CI_Controller {
public function index($parameterValue=null)
{
//do whatever you need here
//note the default value just in case it is null
}
}
If you want to make things shorter, you will have to define an alias in the config/routes.php file. Also, if you want to get rid of the index.php, you will have to configure your web server accordingly (either through .htaccess if using apache or web.config if using iis)

Can't access a Codeigniter controller in sub folder

I have set up a folder with a controller in it: controllers/admin/home.php, but I get a 404 from the browser when I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess seems fine standard. Any ideas on what i'm doing wrong?
Note some things:
1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by #Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied).
It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc).
Without the additional segments, this would by default load the index method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

codeigniter controller

I want to write a new controller file, for example:
aaa.php
class aaa extends CI_Controller
{
public function bbb()
{
// Stuff
}
}
how can i enter aaa.php's bbb(),
The example files are begin with welcome.php's index() function.
how can I change that to begin with my new controller file?
If you provide nothing to the base URL, CI will always assume you want the index action. Like localhost/foo will call foo's index() action. With localhost/foo/bar, you will call foo's bar() action. If you want to call localhost and you want to access foo's index(), you need to check that $route['default_controller'] = 'foo'; is correctly setup in your config.php. (If that's not working, check the .htaccess and the index.php to add it manually)
You want to have a separate function run as the Controller's default function? Why not just call this separate function from index()? Beyond this, I'm not really sure what you're asking...the CodeIgniter user_guide is rather extensive if you haven't looked through it.
If you want to use the bbb function of the aaa controller, you just enter this in the url:
www.mysite.com/aaa/bbb/
As Gsto said, to call bbb function enter the url: mysite.com/aaa/bbb
If you want mysite.com/aaa to call bbb() instead of index() by default, your going to want to create the _remap() function in aaa.php controller to call bbb() instead.
See: CI Controllers - Functions Docs
The way to access your controller's methods in CodeIgniter is by uri. The default routing is:
example.com/controller/function/param1/
So to access aaa's bbb() method, you should access the following uri:
/aaa/bbb
If you want to set aaa's bbb() method as the default page of you application, there is two things to do.
You must first tell CodeIgniter to set aaa as your default controller
/* /application/config/routes.php */
$route['default_controller'] = "aaa";
After that, the aaa's index() method will be call by accessing your base site url. You can't tell CodeIgniter to change de default method index() to something else (without setting some routes), so the easiest way to call bbb() by default would be that:
/* /application/controllers/aaa.php */
class aaa extends CI_Controller
{
public function index()
{
$this->bbb();
}
public function bbb()
{
// Stuff
}
}

Categories