hyperf / nano

🧬 Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.

Home Page:https://nano.hyperf.wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Implementation of Hyperf View in Nano

ljfreelancer88 opened this issue · comments

commented

Hi, how can I implement the Hyperf view to render page in Nano? Since the structure of configuration of Hyperf and Nano is different.

Thanks in advance.

/assign @Reasno

It is no different from standard hyperf.

    $app->config([ 'view' => [ 'engine' => BladeEngine::class]);
    $app->get('/', function(RenderInterface $render){
        return $render->render('index', ['name' => 'Hyperf']);
    });

Remember to require hyperf/view first.

commented

Thank you but how about the static resources configuration? Where to put this exactly?

commented

I got also this Missing Task Worker processes, please set server.settings.task_worker_num before use task. error. But I'm not sure where to set this.

Put all relevant configurations in $app->config(). They follow the same structure in standard hyperf, except that the top-level keys are mapped from file names.

For example

$app->config([
    'view' => [
         'engine' => BladeEngine::class
    ],
    'task' => [
        'settings' => [
            'task_worker_num' => 8,
            'task_enable_coroutine' => false,
        ],
        'callbacks' => [
            // Task callbacks
            SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
            SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
        ],
    ],
    'server' => [
        'settings' => [
            'document_root' => BASE_PATH . '/public',
            'enable_static_handler' => true,
        ],
    ]
]);
commented

I've tried to configuration below but got this Missing Task Worker processes, please set server.settings.task_worker_num before use task error.

$app->config([
    'db.default' => [
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', 3306),
        'database' => env('DB_DATABASE', 'nano'),
        'username' => env('DB_USERNAME', 'username'),
        'password' => env('DB_PASSWORD', 'password'),
    ],
    'view' => [
    	'engine' => BladeEngine::class,
    ],
    'task' => [
        'settings' => [
            'task_worker_num' => 8,
            'task_enable_coroutine' => false,
        ],
        'callbacks' => [
            SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
            SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
        ],
    ],
    'server' => [
        'settings' => [
            'document_root' => __DIR__ . '/public',
            'enable_static_handler' => true,
        ],
    ]
]);

'engine' => BladeEngine::class,
'mode' => Mode::SYNC,

hyperf/task is required unless you use 'mode' => Mode::SYNC,

commented

Sorry but 'mode' => Mode::SYNC doesn't work. Still giving me the same error.

I see no reason why it is not working. Here is a working/tested demo: https://github.com/Reasno/nano-demo

commented

I've tried your demo, still got the same error. I switched back trying the basic example, it's weird because it suppose to work but again the same error.

Screenshot from 2020-07-17 10-44-58

composer remove hyperf/task

commented

composer remove hyperf/task works! Thank you. Greatly appreciated your help guys.