Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Symfony sub domain routing

How can we use sub domains to access sub folders in Symfony.

The question is, how can I have a sub domain routed to a folder?

example.com         -> example.com/*
app.example.com     -> example.com/app/*
admin.example.com   -> example.com/admin/*
api.example.com     -> example.com/api/*

Some detailed examples:

url requested           controller called           route name
----------------------------------------------------------------
example.com/            example.com/                main_home
example.com/login       example.com/login           main_login

app.example.com/        example.com/app/            app_home
app.example.com/profile example.com/app/profile     app_profile

So far hard coding each controller works

Symfony: @Route("/", name="app_home", host="example.com")
Symfony: @Route("/", name="sub1_home", host="sub1.example.com")

But I haven't found a way of hiding the sub folder. So to access the profile page you still need to go to app.example.com/app/profile. Which defeats the purpose of the subdomain having /app in the url.

And yes, obviously any subdomain would have the sub folders restricted in the whole app. So you couldn't have example.com/api/ because that would be reserved for api.example.com

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

@Bradmage why do you complicate what doesn't need to be complicated ? Having

app.example.com     -> example.com/app/*
admin.example.com   -> example.com/admin/*
api.example.com     -> example.com/api/*

will thriple the authorization url because You will need to add login route for each etc. and probably the other things also.

Why don't You leave that and point every subdomain to You application (or strict ones) and rely on a user roles ?

Also You can check up access_control in symfony and host parameter: https://symfony.com/doc/current/security/access_control.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...