【问题标题】:The @ sign in Laravel web.php route filesLaravel web.php 路由文件中的 @ 符号
【发布时间】:2017-02-13 22:08:39
【问题描述】:

laravel 路由文件 web.php 中的“@”符号究竟代表什么
例如:Route::get('/', 'HomeController@index');?请不要告诉我这段代码是什么,我已经知道了。但问题是为什么@符号??
'@' 符号不是方法,但索引是。那么这个@符号的名称是什么,它到底是做什么的?我只是想知道为什么??? @ 符号。

【问题讨论】:

  • "at"... "HomeController" 表示将要使用的控制器,然后“at”功能...“at index function”。所以,我们将“HomeController@index”翻译成“我想要 HomeController 的 index 函数”。
  • AFAIK 它的唯一目的是将控制器类名与控制器方法名分开。也可以使用冒号或任何其他在类名/命名空间中不可用的字符,但选择了 at 符号。
  • 来自 Router.php:Now we can split the controller and method out of the action string so that we can call them appropriately on the class. This controller and method are in in the Class@method format and we need to explode them out then use them.。为什么@符号?询问开发人员,他们是选择它的人。
  • @JeffLambert 非常感谢!!这是我所期待的答案,所以我现在明白了。再次感谢。

标签: php laravel laravel-5


【解决方案1】:

cmets 大部分都涵盖了它,但我会添加一些注释,因为我认为这是一个有趣的问题。

技术使用

我们可以通过查看源代码看到,它只是一个很好的老式explode 语句,将控制器与方法分开:

继续评论 如果绑定有@符号,我们将假设它被用来分隔 绑定方法名中的类名。这允许绑定 为方便起见,在单个类中运行多个绑定方法。

        $segments = explode('@', $binding);

在技术方面,@JeffLambert 提到的“为什么”是任何不是类或方法名称的有效字符的字符都可以用作分隔符。

非技术性

正如@aynber 所提到的,您需要让开发人员做出具体的猜测。但是我会冒险(非常合理的)猜测:

@ 在编辑器中常用来标记方法。例如,在 sublime 中,打开命令面板并输入@,它会显示当前类/文件中所有方法的列表:

我还认为它读起来很自然。使用controller“在”this method

【讨论】:

    猜你喜欢
    • 2018-12-22
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2021-12-03
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多