问题描述
我正在更新我编写的PHP框架。它过去只是使用默认行为进行路由。例如,考虑请求转到 domain.com/package/controller/method
...
<$ p的情况$ p>
$ url = [ package, controller, method];
//检查软件包是否存在...
//检查控制器是否存在于软件包中...
//检查方法是否存在于控制器中...
这一切都很好,而且效果很好。但是,我想为路由器添加一些其他功能。该功能是定义自定义路由并传递匿名功能的功能,该功能可以完成您想要的任何事情。
但是,假设请求与任何用户都不匹配,定义的路由,我想使用我现在拥有的默认功能来检查是否还有其他可能的路由。这样,我可以使用新框架更新旧项目而不会破坏它们,此外...我喜欢这种默认行为,因为大多数时候路由并不那么复杂,并且定义路由对我而言就像违反DRY一样。
问题是我不想将用户定义的路由作为数组传递给对象构造函数。相反,我希望用户将其作为基础应用程序对象上的方法调用,类似于laravel或express处理此方法的方式。
问题是我想要默认的路由检查之前未检查过用户定义的路线之后发生。这个准代码可以帮助您理解我的意思...
class App
{
__construct
{
//检查默认路由
}
私有函数get()
{
//获取请求
}
私有函数post()
{
//发布请求
}
私有函数put()
{
//放入请求
}
私有函数delete()
{
//删除请求
}
}
app :: get();
在上述情况下,默认路由将在调用用户定义的路由之前进行。我在PHP构造器/析构器页面上查看了,并了解了 __ destruct
。但是,在阅读之后,我有点不确定这是否可行。
PHP.net表示...
解释的第一部分听起来完全像我想要的。即一旦在应用程序对象上调用了所有方法,我们将运行 __ destruct
函数,该函数将检查用户定义的路由是否有效,如果不成功, ,检查默认路由系统是否产生任何结果。
问题是我不确定这是不好的做法,还是根本行不通。我可以要求一个文件,设置我的控制器,然后从 __ destruct
内在该控制器上调用一个方法吗?是否有限制会影响这些控制器中的代码?假设使用 __ destruct
这样会出现问题,我有什么选择,请记住我不喜欢上述任何一种解决方案...
- 让用户在脚本末尾调用默认路由作为方法。
- 将路由作为数组传递给构造函数。
我觉得你在这里很困惑。请在PHP手册中注意这一点
换一种说法,调用析构函数有两个原因
- 该类正在被垃圾回收。换句话说,您已覆盖或取消。这意味着您不能再直接调用该类
- PHP脚本已结束,线程正在关闭。
换句话说,该课程没有什么可做的。但是,在您自己的声明中,您说的是这样
这里没有和 。这才是重点。实际上,。
您需要的是分层思考。因此,您将需要一个控制器层来检查方法。反过来,该控制器将打开一个新层,用于检查用户功能。该类或方法应该返回某些内容,或者如果失败则抛出 Exception
。如果失败,则可以尝试使用默认方法。这就是构造程序的方式。尝试使用析构函数执行此操作可能只会使人们感到困惑。使数据流显式而不是隐式(这是魔术方法的作用)。
I'm updating a PHP framework I've written. It used to just use a default behavior for routing. For example consider a case where the request goes to domain.com/package/controller/method
...
$url = ["package", "controller", "method"];
//Check if package exists...
//Check if controller exists in package...
//Check if method exists in controller...
This is all well and good, and works perfectly. However, I wanted to add some additional functionality to my router. That functionality being the ability to define custom routes, and pass an anonymous function which does whatever you want.
However, supposing that the request does not match any of the user-defined routes, I want to use the default functionality I have now to check if there are additional possible routes. That way I can update old projects with the new framework and not have them break, and additionally...I just like this default behavior because most of the time routes are not that complicated and defining routes feels like a violation of DRY to me.
The problem is that I don't want to pass the user-defined routes as an array to the object constructor. Rather, I want the user to call them as methods on the base application object similar to how laravel or express handles this.
The problem is that I want the default route checking to happen AFTER the user's defined routes have been checked not before. This quasi-code might help you understand what I mean...
class App
{
__construct
{
//Check Default Routing
}
private function get()
{
//Get Request
}
private function post()
{
//Post Request
}
private function put()
{
//Put Request
}
private function delete()
{
//Delete Request
}
}
app::get();
In the above case, the default routing would take place before the user-defined routes are called. I looked at the PHP consrtuctor/destructor page and learned about __destruct
. However, after reading this question I'm a little bit unsure this would work.
PHP.net says...
The first part of that explanation sounds like exactly what I want. I.E. as soon as all of the methods have been called on the application object, we'll run the __destruct
function which will check if the user-defined routes were fruitful, and if not, check if the default routing system yields any results.
The problem is that I'm not sure if this is bad practice, or simply won't work. Can I require a file, set my controller, and then call a method on that controller from within __destruct
? Are there limitations that would effect the code within these controllers? Supposing that there is a problem using __destruct
this way, what are my alternatives, keeping in mind I don't like either of these solutions...
- Having the user call the default routing as a method at the end of their script.
- Passing routes in as arrays to the constructor.
I think you're confused here. Take note of this from the PHP Manual
To put this a different way, there's two reasons to call a destructor
- The class is being garbage collected. In other words, you've overwritten or
unset
all the references to the class instance. This means you can't directly call this class anymore - The PHP script has reached its end and the thread is shutting down.
In other words, there's nothing left for the class to do. But in your own statement you say this
There is no "and" here to work with. That's the point. In fact, there's very few places you would use this.
What you need is to think in layers. So you'd have a controller layer that you'd call to check the methods. In turn, that controller opens a new layer that checks user functions. That class or method should return something or throw an Exception
if it fails. On failure it can then try to use default methods. This is how you need to structure your program. Trying to use a destructor to do this would likely only confuse people. Make the data flow explicit, not implicit (which is what magic methods do).
这篇关于使用`__destruct`来实现默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!