问题
I am building a site ,which requires a unique sub-domain for each profile. The technology which I am using is Cake PHP + MongoDB. I need to know the feasibility of the same. Whether I can create a Physical DNS or I can simply route the Profile page of the User , by the help of Routes Class in Cake PHP. Please suggest what will be better and easy way to implement along with feasibility of the same.
Thanks in Advance.
回答1:
If you are using CakePHP for each of the websites, you do not need to worry about multiple subdomain creation for each site, you can simply create a wildcard DNS record. This removes a lot of the hassle from the server and DNS administration.
Wild-Card DNS Record(Wiki)
With the wildcard DNS record in place, you can then use HTTP_HOST, or the CakePHP way, $request->host() to determine the hostname (subdomain) being used to access your system. You can then load the correct profile details in the beforeFilter() method of AppController and move forward from there.
I do something simila:
class User extends AppModel
{
public $belongsTo = array(
'Profile'
);
public function findByProfileHostname($hostname) {
return $this->find('first', array('conditions' => array(
'Profile.hostname' => $hostname
));
}
};
class AppController extends Controller
{
public function beforeFilter () {
$this->ActiveUser = $this->User->findByProfileHostname($this->request->host());
if (!$this->ActiveUser) {
throw new InvalidArgumentsException(__('%s is not active here', $this->request->host()));
}
}
}
*disclaimer: Above code has been typed from memory and may not work directly through copy/paste.
Hope this helps.
回答2:
It's easily doable! A little bit of scripting and hosting configuration is all thats needed:
Let's assume, ignoring the subdomains for now, that you can access the user profiles at yoursite.com/users/view/id - standard convention.
All we then need to do is map username.yoursite.com to the correct yoursite.com/users/view/id - and this should happen any time a user signs up. So in the users/add - we need to write a script that can access your servers 'users' / 'sites-available' (or equivalent) - and either append to the end of the already existing 'yoursite.com' configuration, or make new 'username.yoursite.com' every time a user signs up.
Now the annoying bit - on the server your app will need permission to read/write to the servers configuration folder, plus server also need to be restarted for a new conf file - my sysadmin skills only go so far and you'll have to find out; by running a bash script or something else on how to get the server to read the new conf files without getting it to restart.
Also make sure the server files update if the username is changed or the user is deleted.
回答3:
First of all. The creation of subdomains has nothing to do with Cake, Mongo or PHP. This is a DNS thing and you need to create correct DNS entries. Ex:
subdomain1 IN CNAME domain.com.
subdomain1 IN A 123.2.33.45.
As @e-sushi was pointing out this takes time. You can automatize this process, but not with PHP but with root access (you can actually do it in PHP with shell_exec but that is another story). Depending on your hosting this can be actually quite complicated and unreliable in terms of time.
Imagine that following scenario. A user is created, but he will be able to access is webpage somewhere between 6 and 24 hours after. You will have to create some temporary path for the time being and a full scenario with what happens with the data and how the links are pointing... etc. Too complicated.
The routing mechanisms from Cake is a helper mechanism that can transform a request and solve it to another path using an internal logic. So it can take something like:
yourwebsite.com/user/1
And transform it to:
joe.yourwebsite.com
But this process is independent of the creation and maintenance of the subdomains.
回答4:
In short:
Auto-create subdomains using Cake PHP? Sorry, but that won't work.
Longer:
You won't get where you want to go using the Routes Class in Cake PHP. "Routes" allow configuration of paths, but it won't create paths or subdomains. As said: you're talking two different things: Cake PHP's Routes Class = PHP based rewrites, a subdomain = DNS configuration of the server.
Before you even waste time thinking about DNS, be aware that new DNS entries may take up to 24 hours to kick in. Before a new (sub)domain can be used, it has to be propagated. Are you sure that's what you want? It could mean (thinking about user registration) that new users could register but not use or even reach their "new" subdomain, because it simply isn't known to several DNS servers yet.
But... you could try to make wildcard subdomains work if the waiting time is no problem to you. But that heavily depends on your web-hosting provider and what they allow you to do. In case of doubt, contact them (after you've learned what wildcard DNS does and if you really want to use it).
Similar questions and answers can be found here:
- how do i create personal sub domain programmatically with php
- How would you create sub-domains with DNS automatically?
But I can only give you a good tip: think twice before starting to go down this road. It might get too bumpy for you and your users and then you'll have wasted valuable time you could've invested better in working out alternate options. Besides, what's wrong with a decent directory structure? You know, search-engines are known to love decent directory structures more than subdomain-setups as directory structures are more logic when all the data is related to a single website. Even if that website is something big... like Facebook or Twitter. ;)
来源:https://stackoverflow.com/questions/6744733/how-to-create-sub-domains-using-cake-php