开发者

codeigniter controller

开发者 https://www.devze.com 2023-01-17 06:09 出处:网络
I want to write a new controller file, for example: aaa.php class aaa extends CI_Controller { public fun开发者_JAVA技巧ction bbb()

I want to write a new controller file, for example:

aaa.php

class aaa extends CI_Controller
{
    public fun开发者_JAVA技巧ction 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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消