PHP code example of 68publishers / smart-nette-component

1. Go to this page and download the library: Download 68publishers/smart-nette-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

68publishers / smart-nette-component example snippets


use Nette\Application\UI\Presenter;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\AuthorizationTrait;

abstract class BasePresenter extends Presenter
{
    use AuthorizationTrait;
}

use Nette\Application\UI\Control;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\AuthorizationTrait;

abstract class BaseControl extends Control
{
    use AuthorizationTrait;
}

use SixtyEightPublishers\SmartNetteComponent\Attribute\LoggedIn;
use SixtyEightPublishers\SmartNetteComponent\Attribute\Allowed;

#[LoggedIn]
final class AdminProductPresenter extends BasePresenter
{
    #[Allowed('product_resource', 'add')]
    public function actionAdd(): void {}

    #[Allowed('product_resource', 'delete')]
    public function handleDelete(): void {}
}

use SixtyEightPublishers\SmartNetteComponent\Attribute\LoggedIn;
use SixtyEightPublishers\SmartNetteComponent\Attribute\Allowed;

final class EditOrderControl extends BaseControl
{
    #[Allowed('order_resource', 'delete_item')]
    public function handleDeleteItem(): void {}
}

protected function onForbiddenRequest(ForbiddenRequestException $exception): void
{
    # `$exception->rule` contains failed attribute
    
    $this->flashMessage('You don\'t have access here!', 'error');
    $this->redirect('Homepage:');
}

use SixtyEightPublishers\SmartNetteComponent\Attribute\AttributeInterface;
use SixtyEightPublishers\SmartNetteComponent\Authorization\RuleInterface;
use SixtyEightPublishers\SmartNetteComponent\Authorization\RuleHandlerInterface;
use SixtyEightPublishers\SmartNetteComponent\Exception\ForbiddenRequestException;

final class CustomRule implements AttributeInterface, RuleInterface
{
    # ...
}

final class CustomRuleHandler implements RuleHandlerInterface
{
    public function canHandle(RuleInterface $rule): bool
    {
        return $rule instanceof CustomRule;
    }

    public function __invoke(RuleInterface $rule): void
    {
        assert($rule instanceof CustomRule);

        if (...) {
            throw new ForbiddenRequestException($rule);
        }
    }
}

use Nette\Application\UI\Control;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\TemplateResolverTrait;

abstract class BaseControl extends Control
{
    use TemplateResolverTrait;
}

final class MyControl extends BaseControl
{
    protected function beforeRender(): void
    {
        # assign variables into the template here
    }

    public function renderFoo(): void
    {
        $this->doRender('foo');
    }
}

final class MyPresenter extends BasePresenter
{
    protected function createComponentMyControl() : FooControl
    {
        $control = $this->myControlFactory->create();

        $control->setFile(__DIR__ . '/path/to/file.latte');
        # or relatively from a component's directory:
        $control->setRelativeFile('templates/new/myControl.latte');

        # you can change the template for a specific render type:
        $control->setFile(__DIR__ . '/path/to/myControl.latte', 'foo'); # template for `renderFoo()`

        return $control;
    }
}