使用`__destruct`来实现默认路由? [英] Using `__destruct` to implement default routing?

查看:112
本文介绍了使用`__destruct`来实现默认路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新我编写的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手册中注意这一点


只要没有其他对特定对象的引用,就会调用destructor方法,或者在关闭序列中以任何顺序排列。


换一种说法,调用析构函数有两个原因


  1. 该类正在被垃圾回收。换句话说,您已覆盖或<重置了所有对类实例的引用取消。这意味着您不能再直接调用该类

  2. PHP脚本已结束,线程正在关闭。

换句话说,该课程没有什么可做的。但是,在您自己的声明中,您说的是这样


解释的第一部分听起来很像我想要的。即一旦在应用程序对象上调用了所有方法,我们将运行__destruct函数,该函数将检查用户定义的路由是否有效,,否则,检查默认路由是否有效系统会产生任何结果。


这里没有。这才是重点。实际上,在此使用的地方很少了。



您需要的是分层思考。因此,您将需要一个控制器层来检查方法。反过来,该控制器将打开一个新层,用于检查用户功能。该类或方法应该返回某些内容,或者如果失败则抛出 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 destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

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

The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

To put this a different way, there's two reasons to call a destructor

  1. 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
  2. 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

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.

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`来实现默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆