1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Jidaikobo\Kontiki\Core\Database; |
4
|
|
|
use Jidaikobo\Kontiki\Core\Auth; |
|
|
|
|
5
|
|
|
use Jidaikobo\Kontiki\Models\UserModel; |
6
|
|
|
use Jidaikobo\Kontiki\Models\PostModel; |
7
|
|
|
use Jidaikobo\Kontiki\Services\ValidationService; |
8
|
|
|
|
9
|
|
|
if (!function_exists('getIndex')) { |
10
|
|
|
/** |
11
|
|
|
* @param array $args Configuration for the request. |
12
|
|
|
* @param string $env environment. |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
function getIndex(array $args, string $env = 'production'): array |
16
|
|
|
{ |
17
|
|
|
$app = Jidaikobo\Kontiki\Bootstrap::init($env); |
18
|
|
|
$container = $app->getContainer(); |
19
|
|
|
$model = new PostModel( |
20
|
|
|
$container->get(Database::class), |
21
|
|
|
$container->get(ValidationService::class), |
22
|
|
|
$container->get(Auth::class), |
23
|
|
|
$container->get(UserModel::class) |
24
|
|
|
); |
25
|
|
|
$retval = []; |
26
|
|
|
$retval['body'] = $model->getIndexData('published', $args); |
27
|
|
|
$retval['pagination'] = $model->getPagination(); |
28
|
|
|
return $retval; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!function_exists('getData')) { |
33
|
|
|
/** |
34
|
|
|
* @param array $args Configuration for the request. |
35
|
|
|
* @param string $env environment. |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
function getData(array $args, string $env = 'production'): array |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$app = Jidaikobo\Kontiki\Bootstrap::init($env); |
41
|
|
|
$container = $app->getContainer(); |
42
|
|
|
$model = new PostModel( |
43
|
|
|
$container->get(Database::class), |
44
|
|
|
$container->get(ValidationService::class), |
45
|
|
|
$container->get(Auth::class), |
46
|
|
|
$container->get(UserModel::class) |
47
|
|
|
); |
48
|
|
|
$slug = $args['slug'] ?? ''; |
49
|
|
|
$retval = []; |
50
|
|
|
$retval['body'] = $model->getByFieldWithCondtioned('slug', $slug, 'published'); |
51
|
|
|
return $retval; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: