A basic Hello World component example?
- Yeti
- Member | 8
I can't seem to get my head around creating a basic component without coming across an error such as “Component with name ‘helloControl’ does not exist”.
Is there a basic complete “Hello World” example around that I could look at? Just something that prints “Hello World” from any views that include {control hello}.
So far I have the folder App/Components with a file helloControl.php that contains the following
<?php
declare(strict_types=1);
namespace App\Components;
use Nette\Application\UI\Control;
class HelloControl extends Control
{
// Not sure what to do here for example to print a simple "Hello World"
// message to any template that includes {control hello}
}
I added it as a service
- App\Components\HelloControl
But that's where I get stumped and the errors start to appear
Last edited by Yeti (2023-09-21 03:10)
- mystik
- Member | 308
Where and how do you use that component? If you want to use component in some presenter you have to add component creation method to that presenter
public function createComponentHello(): HelloControl {
return new HelloControl();
}
Component instance is created automatically by calling this presenter method first time component is used. If this method is missing component cannot be instantiated and you get that error.
In component itself you should add method render() that is called when
component is rendered. It can be simple echo "Hello world"
Last edited by mystik (2023-09-21 08:51)
- materix
- Backer | 83
Thanks for the reply, which I guess also answers this question of mine: https://forum.nette.org/…ide-of-nette.
The Nette Component-Model is primarily used for Nette Presenters, right? 🙂
- dsar
- Backer | 53
I see components as a reusable pieces of software in the small, while extensions as a reusable pieces of software in the large.
Other frameworks only offer the latter (for example Symfony uses bundles) and you have no way (or complicated way) to have reausability within a single project. Using bundles for simple tasks is just overkilling.
In my opinion using components outside Nette doesn't make much sense because they are integrated in the whole framework (you have reusability especially with renderable components)
- Yeti
- Member | 8
Ok thanks for the reply but I am now getting the following error
Class "App\Presenters\HelloControl" not found
(Not sure why
it's not looking in App\Controllers)
Again I have - App\Components\HelloControl
added to common.neon
and the HelloControl.php file in app\Components has been updated to the
following
declare(strict_types=1);
namespace App\Components;
use Nette\Application\UI\Control;
class HelloControl extends Control
{
public function render(): void
{
echo "Hello World";
}
}
Now in the presenter I have the following
declare(strict_types=1);
namespace App\Presenters;
final class SomePresenter extends BasePresenter
{
public function createComponentHello(): HelloControl {
return new HelloControl();
}
public function renderIndex(): void
{
}
}
And finally in the index.latte template I have
{control hello}
I am obviously missing something somewhere?
- Yeti
- Member | 8
mystik wrote:
You are missing
use App\Components\HelloControl
in your presenter so HelloControl is interpreted as class in App\Presenters namespace.Adding component to neon is useless. You create instance in createComponent method not using ot from DI container.
That was indeed the problem. Thanks a lot for all your help!