我是Symfony的新手,我不确定如何最好地构建我的Web项目.该解决方案必须包含3个用例:
所有三个虚拟主机都指向Symfony/web目录
问题:
这是我的Symfony项目中的3个独立应用程序(例如"前端","后端"和"管理员"或"公共","成员","管理员")?
或者,这个应用程序是否包含上述每个用例的模块?
编辑:我无法访问apache中的httpd.conf来指定虚拟主机的默认页面.我只能使用hostin提供程序的cPanel为每个子域指定一个目录.
如果不了解每个域/ app /的实际职责,这很难说.这是您必须根据项目的要求回答的问题......没有一种策略可以推荐用于每个用例.
据说我认为最多你有两个应用程序 - 一个作为前端,还包括"成员"功能.我认为这些可能是一个罪恶的应用程序的共鸣是因为你想要生成一个来自另一个的链接(如果它们是单独的应用程序,这是非常难以做到的) - 即.每个人都可以访问主页并说出常见问题解答,但只有会员可以访问下载或其他内容,但它们仍然是同一个站点.
另一个应用程序将用于后端并保留管理功能.无论有多少应用程序可以共享同一个web目录,只需创建一个符号链接,然后适当地指向apache,例如:
cd htdocs
ln -s SF_ROOT_DIR/web frontend
ln -s SF_ROOT_DIR/web backend
Run Code Online (Sandbox Code Playgroud)
现在,您可以指向您的cPanel设置htdocs/frontend为domain.com和members.domain.com和点admin.domain.com到htdocs/backend.
然后你可以改变你的.htaccess看起来像这样:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
# if subdomain is admin.* then use backend.php application
RewriteCond %{SERVER_NAME} ^admin\..*$
RewriteRule ^(.*)$ backend.php [QSA,L]
# not admin so we use the frontend app
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)
这样您就可以使用no_script_name这两个应用程序.
另一种做同样事情的方法是将index.php修改为如下所示:
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
if(0 === strpos($_SERVER['SERVER_NAME'], 'admin'){
$app = 'backend';
}
else
{
$app = 'frontend';
}
$configuration = ProjectConfiguration::getApplicationConfiguration($app, 'prod', false);
sfContext::createInstance($configuration)->dispatch();
Run Code Online (Sandbox Code Playgroud)
您可以根据需要修改应用名称检测但是您可以理解.
使用这两种方法,您可以基于子域执行一些基本确定,并且您可以应用此策略,无论我们谈论的应用程序数量是什么,只要您使用我推荐的2或使用3.
改变是做一个家庭的申请.假设每个应用程序实际上只是整个应用程序的一个方面,我喜欢这条路线.但是,我会考虑成员,非成员和管理员对模块的定义过于宽泛.
如果你这样做,你可能最终会在控制器中进行大量的操作.相反,我会针对实际问题制作单独的模块.此外,没有真正的理由在这里使用子域 - 更容易使用URL的一部分(即./ admin或/ members)来使用特定的模块.
例如,让我们采取用户......通常情况下,这将是一个管理区域,因此我们可以使用管理生成器来实现该功能,并调用模块UserAdmin或类似的东西.然后,对于userland的东西,我们可能只有一个模块User,可以处理公共配置文件查看和列表,用户的配置文件编辑和所有这些东西.然后,对于实际登录,我们可能有一个UserAuth严格处理登录/注销和忘记密码请求等内容的模块.您可以将任何URL路由到这些模块中的任何一个,因此您的路由可能如下所示:
user_login:
url: /login
params: {module: UserAuth, action: login}
user_logout:
url: /logout
params: {module: UserAuth, action: logout}
user_password:
url: /forgot-password
params: {module: UserAuth, action: recoverPassword}
user_reset:
url: /password/reset/:token
params: {module: UserAuth, action: resetPassword}
user_profile:
url: /members/:username
params: {module: user, action: showProfile}
user_index:
url: /members
params: {module: User, action: index}
user_default:
url: /members/:action
params: {module: User}
admin_user:
class: sfDoctrineRouteCollection
options:
Module: UserAdmin
model: User
prefix_path: /admin/user
Run Code Online (Sandbox Code Playgroud)
以这种方式执行操作的最后一步是确保使用security.yml中的正确凭据要求保护适当的模块和操作,并且还要适当地分配凭据.