Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

php - Subdomains leading to Codeigniter Controllers?

This seems like a common request, but I haven't been able to find definitive instructions on doing something like this.

I'd like a subdomain to trigger a certain controller on my CI installation. For example:

students.mysite.com : would open mysite.com/students (technically: mysite.com/index.php/students. controller: students)

teachers.mysite.com : would open mysite.com/teachers

While preserving the subdomain when traversing deeper. For example:

students.mysite.com/help : would open mysite.com/students/help (controller: students(), method: help())

students.mysite.com/help/contact : would open mysite.com/students/help/contact (controller: students(), method: help(), argument: "contact")

students.mysite.com/help/contact/email : would open mysite.com/students/help/contact (controller: students(), method: help(), arguments: "contact", "email")

I realize that something.mysite.com right now returns an error. So I figure:

Step 1 would be allowing anything.mysite.com to return the root (mysite.com/index.php)

Step 2 would be reading the subdomain and calling that controller

Step 3 would be reading the first argument after the first "/" and calling that method of the controller, and passing the remaining url parts as arguments

I guess really I'm stumped at Step 1. I'm on a shared hosting account, is this something I can do via CPanel? I tried adding a subdomain for *.mysite.com without any luck (unless I just needed to wait longer for propogation, but I feel the chances are higher that I got it wrong).

Back on my home WAMP installation, I'd change up httpd.conf, right? Can I acheive this effect without modifying that file (since I probably can't, since I'm shared using webhostinghub.com)

Phew, thanks for your time! - Keith

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you want to use a particular domain to lead to your controllers, what I came up with was using the application/config/routes.php file to achieve it. The idea is load different controllers depending on what subdomain you use, so, instead of writing a list of routes for your domain, you write a list of routes DEPENDING on the domain you're accessing from:

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'students.mysite.com':
        $route['default_controller'] = "students";
    break;
    case 'teachers.mysite.com':
        $route['default_controller'] = "teachers";
    default:
        // The list of your $routes lines at is was...
    break;
}

To make this work, you only have to point the subdomain to your CI project (Dwayne Towell in the step 1 of the other answer explains how to do it perfectly) and you'll have everything working, your shared hosting won't be a problem and you won't have to configure the server.

UPDATE

After reading this answer, check the answer from @Josh (https://stackoverflow.com/a/47368922/1168804) as it offers a wonderful way to organize the routing code to avoid unexpected routing behaviour with the controllers. It's worthy reading it (and upvoting it, ;D)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...