Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Application Laravel Package

nette/application

Nette Application is the web framework core of the Nette stack, providing MVC architecture with presenters, routing, DI integration, HTTP handling, and error/debug support. Build secure, structured PHP web apps with clean separation of concerns.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup for Laravel Integration

  1. Installation

    composer require nette/application nette/routing nette/di
    

    Note: Laravel’s built-in DI container won’t work directly; use Nette\DI\Container or bridge it via Laravel’s ServiceProvider.

  2. Front Controller (public/index.php)

    require __DIR__.'/../vendor/autoload.php';
    
    $container = new Nette\DI\Container();
    $router = new Nette\Application\Routers\RouteList();
    $router[] = $router->addRoute('<presenter>/<action>', 'Homepage:default');
    
    $application = new Nette\Application\Application(
        $container,
        $router,
        new Nette\Application\UI\PresenterFactory()
    );
    $application->run();
    
  3. First Presenter (app/Presenters/HomepagePresenter.php)

    namespace App\Presenters;
    
    use Nette\Application\UI\Presenter;
    
    class HomepagePresenter extends Presenter {
        public function renderDefault() {
            $this->template->message = 'Hello, Laravel + Nette!';
        }
    }
    
  4. Template (app/Presenters/templates/Homepage/default.latte)

    {layout @layout}
    <h1>{$message}</h1>
    
  5. Laravel Route Proxy (Optional) In routes/web.php, proxy requests to Nette’s front controller:

    Route::get('/{path?}', function ($path = '') {
        return app('nette.application')->run();
    })->where('path', '.*');
    

Implementation Patterns

1. Presenter-Based Routing

  • Convention: Presenter:action → maps to Presenter.php and Presenter/templates/action.latte.
  • Dynamic Routing:
    $router[] = $router->addRoute(
        '<presenter>/<action>/<id>',
        'Homepage:show',
        ['id' => '[0-9]+']
    );
    
  • Signal Handling (vs. Laravel’s events):
    // In Presenter
    public function handleSubmit() {
        $this->getComponent('form')->onSuccess[] = [$this, 'formSucceeded'];
    }
    
    public function formSucceeded($form) {
        $this->flashMessage('Success!');
    }
    

2. Dependency Injection

  • Nette DI vs. Laravel:
    // Nette DI (app/presenters.di.php)
    $container->addService('database', new Nette\Database\Connection($dsn));
    
    // In Presenter
    public function __construct(Nette\Database\Connection $db) {
        $this->db = $db;
    }
    
  • Laravel Bridge: Use Nette\DI\Container as a Laravel service provider:
    class NetteServiceProvider extends ServiceProvider {
        public function register() {
            $this->app->singleton('nette.application', function () {
                return new Nette\Application\Application(
                    new Nette\DI\Container(),
                    new Nette\Application\Routers\RouteList(),
                    new Nette\Application\UI\PresenterFactory()
                );
            });
        }
    }
    

3. Template Inheritance

  • Latte Templates (vs. Blade):
    {extends @layout}
    {block content}
        <p>Dynamic content via {template variables}.</p>
    {/block}
    
  • Dynamic Components:
    {control user/avatar}
    

4. Forms Integration

  • Nette Forms (vs. Laravel Collective):
    $form = new Nette\Application\UI\Form;
    $form->addText('name', 'Name:');
    $form->onSuccess[] = [$this, 'processForm'];
    
    // In template
    {control form}
    

5. Error Handling

  • Custom Error Presenters:
    $application->setErrorPresenter('Error4xxPresenter');
    
  • Flash Messages:
    $this->flashMessage('Error!', 'warning');
    // In template: {flashMessages}
    

Gotchas and Tips

1. Laravel-Nette Integration Pitfalls

  • DI Container Conflict:

    • Nette’s Container is not Laravel’s Container. Use a facade or bridge:
      $netteContainer = new Nette\DI\Container();
      $this->app->instance('nette.container', $netteContainer);
      
    • Tip: Prefer Nette\DI\Container for Nette-specific services and Laravel’s container for Laravel services.
  • Route Overlap:

    • Laravel’s routes and Nette’s RouteList both process requests. Use middleware to delegate:
      Route::get('/nette/{path}', function ($path) {
          return app('nette.application')->run();
      })->middleware('nette');
      

2. Template and Presenter Quirks

  • Template Naming:

    • Nette expects Presenter/templates/ActionName.latte. Laravel’s views/ won’t work directly.
    • Fix: Symlink or configure Presenter::setTemplateDirectory().
  • Signal vs. Event:

    • Nette’s onSignal[] is not Laravel’s Event::dispatch(). Use a bridge:
      // Convert Nette signal to Laravel event
      $this->onSignal[] = function ($signal) {
          event(new NetteSignalEvent($signal));
      };
      
  • Persistent Parameters:

    • Nette’s @Persistent (e.g., #[Persistent] public int $page = 1) persists across requests.
    • Laravel Alternative: Use session() or cookie().

3. Debugging and Tracy

  • Tracy Integration:

    • Nette includes Tracy Debugger by default. Enable in index.php:
      if (APP_DEBUG) {
          $application->getService('debugger')->enable();
      }
      
    • Laravel Tip: Use tracy/tracy for Laravel debugging alongside Nette.
  • Common Errors:

    • InvalidLinkException: Check Presenter::link() arguments (e.g., Homepage:default vs. Homepage).
    • BadRequestException: Validate Presenter::handle* methods for non-void returns.

4. Performance Tips

  • Caching Templates:
    • Enable Latte caching in ApplicationExtension:
      $extension->templateCache = __DIR__.'/../temp/cache/latte';
      
  • Presenter Factory:
    • Use PresenterFactoryCallback for lazy-loading presenters:
      $factory = new Nette\Application\UI\PresenterFactory();
      $factory->setCallback('Homepage', [$this, 'createHomepage']);
      

5. Extending Nette for Laravel

  • Custom Presenters:
    class ApiPresenter extends Presenter {
        public function renderJson(array $data) {
            http_response_code(200);
            header('Content-Type: application/json');
            echo json_encode($data);
        }
    }
    
  • Middleware Integration:
    $application->addMiddleware(function ($request, $next) {
        // Modify request before Nette processes it
        return $next($request);
    });
    

6. Migration Tips from Laravel

Laravel Concept Nette Equivalent Migration Tip
Route controllers Presenters (Presenter:action) Move logic from Route::get() to Presenter::render*().
Blade templates Latte templates Use {extends @layout} for shared layouts.
Service Container Nette DI ($container->addService()) Prefer constructor injection over app()->make().
Events Signals ($component->onSignal[]) Use a facade to dispatch Laravel events.
Middleware Application::addMiddleware() Wrap Laravel middleware in a Nette callable.
Validation Nette Forms ($form->addRule()) Replace Validator with Nette\Forms\Container.
CSRF Protection Built into Presenter No extra config needed.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope