-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.php
62 lines (55 loc) · 1.76 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
/**
* This file is part of the Inertia library for Hyperf.
*
* @license https://github.com/onix-systems-php/hyperf-inertia/blob/main/LICENSE
*/
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\Arrayable;
use Hyperf\Contract\SessionInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use OnixSystemsPHP\HyperfInertia\Inertia;
use function Hyperf\Collection\collect;
if (! function_exists('inertia')) {
/**
* Inertia helper.
*
* @param null|string $component
* @param array|Arrayable $props
*/
function inertia($component = null, $props = [])
{
/** @var Inertia $inertia */
$inertia = Context::get(Inertia::class, new Inertia());
if ($component) {
return $inertia->render($component, $props);
}
return $inertia;
}
}
if (! function_exists('redirect_with')) {
function redirect_with($path = '/', $data = [])
{
$request = ApplicationContext::getContainer()->get(RequestInterface::class);
$response = ApplicationContext::getContainer()->get(ResponseInterface::class);
$session = ApplicationContext::getContainer()->get(SessionInterface::class);
collect($data)->each(fn ($value, $key) => $session->flash($key, $value));
return $response->redirect(toUrl: $path, schema: $request->getUri()->getScheme());
}
}
if (! function_exists('inertia_location')) {
/**
* Inertia location helper.
*
* @param string $url
*/
function inertia_location($url)
{
/** @var Inertia $inertia */
$inertia = Context::get(Inertia::class, new Inertia());
return $inertia->location($url);
}
}