我使用Interfaces和Traits在CakePHP 3.x中构建更复杂的组件。是否可以将组件类放入单独的子文件夹中,如:
-Controller
---Component
----Comp1
-----Comp11Component.php
-----Comp12Component.php
-----Comp13Component.php
-----Comp1Interface.php
----Comp2
-----Comp21Component.php
-----Comp22Component.php
-----Comp2Interface.php
...
官方文档没有说明我的测试确实失败了。是否有更好的方法来构建CakePHP 3.x中的复杂组件?
答案 0 :(得分:1)
不管这样的结构是否有用,您可以在加载组件时轻松使用className
选项,只要它是可自动加载的,您就可以从任何地方加载任何类。
因此,如果您的组件具有匹配的命名空间,例如App\Controller\Component\Comp1
和App\Controller\Component\Comp2
,则可以像这样加载组件:
$this->loadComponent('Comp1', [
'className' => '\App\Controller\Component\Comp1\Comp11Component'
]);
另请参阅 Cookbook > Controllers > Components > Aliasing Components
答案 1 :(得分:0)
我知道这是一个古老的问题/答案,但是由于我在CakePHP 3.8中尝试了此问题,并且无法使用@ndm他的解决方案使其正常工作,所以我还是会分享我的解决方案。
我知道它是这样工作的:
Option Explicit
Function CheckKeywordAndReplace(ByVal searchKeyword As String, ByVal replaceKeyword As String, ByVal k As Integer) As Boolean
'declare data types
Dim ws As Worksheet, rng_search As Range
'set objects
Set ws = ThisWorkbook.Sheets(3) 'third sheet in the workbook where the macro is stored
Set rng_search = ws.Range("AX" & k)
'if range value is empty
If rng_search.Value = vbNullString Then
'if value has searchKeyword
If InStr(ws.Range("AE" & k).Value, searchKeyword) > 0 Then
'alternatively you can use Like operator, it's commented out
'If ws.Range("AE" & k).Value Like "*" & searchKeyword & "*" Then
'replace value with your keyword
rng_search.Value = replaceKeyword
'update result from False to True
CheckKeywordAndReplace = True
End If
End If
End Function
Comp1Component.php看起来像这样:
$this->loadComponent('Comp1', [
'className' => 'Comp1/Comp1',
]);
和Comp1Interface.php:
namespace App\Controller\Component\Comp1;
use Cake\Controller\Component;
class Comp1Component extends Component implements Comp1Interface {
public function x($y){
return true;
}
}