dongtan1845 2016-09-25 09:44
浏览 448
已采纳

laravel:Route :: group - 如何理解:: here的语法。 因为Route类中没有静态函数`group`

In laravel i am not able to understand what :: means w.r.t the code Route:group or Route:get etc.

As per my understanding :: is used to use static methods from a class.

I check the Route class. but there are no such static methods called group or get. So how to understand.

Of course i need not uderstand that for the starting my app. But to understand the syntax is good always. I am using phpstorm. Generally i can figure out the links of functions and classes. I tried to look for group or get it says not declared.

  • 写回答

1条回答 默认 最新

  • dongluan6784 2016-09-25 10:32
    关注

    When you use Route::method() like that you're using what's called a "facade". The Route class is the facade in this context.

    If you take a look at Illuminate\Support\Facades\Route you'll see that it provides a single static method called getFacadeAccessor, which returns a string router. It's this string that references an instance of the Illuminate\Routing\Router class in the IoC container.

    During it's setup Laravel creates an instance of this class and stores it with router as kind of like a key.

    So when you do Route::get(...), under the hood Laravel is using the facade accessor to find an instance of the Router class that's already been created and calls the get method on it. It uses dynamic programming to achieve this, have a look at the Illuminate\Support\Facades\Facade class for details of what's going on there, in particular the __callStatic method which is where it all starts.

    So, calling Route::get():

    • You're actually calling Illuminate\Support\Facades\Route::get
    • No such method exists on that class, or the Facade class it extends, so the __callStatic method is run.
    • The underlying Facade class calls static::getFacadeRoot() which if you follow the logic, will get you an instance of Illuminate\Routing\Router
    • Finally, the __callStatic method calls $instance->$method(...$args); where $instance is the instance of the router, and $method is get and $args is what you passed to Route::get(...);
    • So when you call Route::get('route', 'Controller@method'); you actually end up calling get('route', 'Controller@method') on an instance of Illuminate\Routing\Router. Notice, you're not actually calling a static method.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算