如何将子域合并到MVC框架中(how to incorporate subdomains into MVC framework)

编程入门 行业动态 更新时间:2024-10-07 20:31:43
如何将子域合并到MVC框架中(how to incorporate subdomains into MVC framework)

随着时间的推移,我开发了自己的框架并且它工作得很好 - 它很轻,速度快,并且经过验证可以处理不错的负载。

现在我正在开发一个可以被描述为在线商店的项目,每个用户都有他的子域名,他的商品可供出售。

我的框架的工作原理是通过/拆分请求URL并确定控制器是什么,动作参数等等。

这工作得很好,但是如何处理子域呢?

我修改了我的请求对象,所以当我输入时可以说:

HTTP://kodi.shop.local/

(我添加了SeverAlias * .shop.local)

这是我的请求对象的样子:

DPLS_Request Object ( [_request] => Array ( [0] => [1] => [action] => defaultAction [controller] => home ) [_params] => Array ( ) [_rurl:private] => kodi.shop.local [_segment] => kodi [get_params] => Array ( ) )

所以_segment是子域名,后来我可以在代码中使用它来验证它与用户名和其他东西,但我甚至在此之前有概念问题。 我的问题是我的框架期望传递一些控制器和动作,并且因为他在URL的末尾获得的是/他假设它显示索引页面:(

那么下一步该做什么...如何在整个mvc控制器/动作故事中加入子域?

一个快速而肮脏的解决方案是修改我的请求类的一部分:

if($this->_request['controller']==''){ $this->_request['controller'] = DEFAULT_CONTROLLER; } if($this->_request['action']==''){ $this->_request['action'] = DEFAULT_ACTION; }

并将其包装在另一个if中以测试是否存在_segment,如果是,则将其分配给控制器DEFAULT _SHOP _CONTROLLER,我将在配置文件中定义为“store”

所以上面的请求类似于键入http://shop.local/store/ (它将运行'store'控制器和默认操作)

在这种情况下你会怎么做? 在处理子域名和控制器和操作时,有没有“最佳实践”?

Over the time i developed my own framework and it's working great - it's light, it's fast and is proven that can handle decent load.

Now I'm working on project that can be described as a online shop where every user will have his sub domain where his items will be available for sale.

My framework works by splitting request url by / and determening what is the controller, actions params.. etc..

and that works just fine, but what to do with subdomains?

i've modified my reqest object, so when i type lets say:

http://kodi.shop.local/

(i added SeverAlias *.shop.local )

this is how my request object look like:

DPLS_Request Object ( [_request] => Array ( [0] => [1] => [action] => defaultAction [controller] => home ) [_params] => Array ( ) [_rurl:private] => kodi.shop.local [_segment] => kodi [get_params] => Array ( ) )

so _segment is the subdomain, and later i can use it in code to validate it vs username and other stuff but i'm having conceptual problems even before that. my problem is that my framework expect some controller and actions to be passed, and because all he gets at the end of URL is / he assumes that it shoud display index page :(

so what to do next ... how to incorporate subdomains in whole mvc controllers/actions story?

one quick and dirty sollution is to modify part of my request class:

if($this->_request['controller']==''){ $this->_request['controller'] = DEFAULT_CONTROLLER; } if($this->_request['action']==''){ $this->_request['action'] = DEFAULT_ACTION; }

and to wrap that in yet another if to test if _segment is present, and if it is then assign to controller DEFAULT _SHOP _CONTROLLER, which i will define in config file as lets say 'store'

so the request above will be analog to typing http://shop.local/store/ (it will run 'store' controller and default action)

what would you do in this case? is there any "best practice" when dealing with subdomains and controllers and actions?

最满意答案

听起来,对我来说,您所描述的方法是实现您添加的功能的合理下一步。 新功能会带来一些额外的复杂性,但是你的Request对象/路由器应该很好地封装“当提供段时我将调用什么控制器?当提供段和控制器时?”的逻辑。 等我说去吧。

It sounds like, to me, the approach you are describing is the logical next step to implement the features you are adding. The new features will bring some added complexity but your Request object/Router should do a good job of encapsulating the logic of "What controller do I invoke when a segment is provided? When a segment AND a controller are provided?" etc. I say go for it.

更多推荐

本文发布于:2023-07-30 15:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   框架   并到   子域合   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!