DocsFullstack frameworks
From Laravel to Nexy
In Nexy a Laravel route and its controller method become one file under src/routes/: a Blade
view becomes a .nexy page, $request->validate a Pydantic model, and Eloquent an ORM you pick,
such as SQLModel.
Each section below shows a Laravel habit, then the same thing in Nexy.
A route
use Illuminate\Support\Facades\Route; Route::get('/hello', fn () => ['message' => 'Hello']);There is no routes file: the file path is the URL, and the function name is the method.
async def GET(): return {"message": "Hello"}Route parameters and where
Route::get('/users/{id}', fn (int $id) => ['id' => $id])->whereNumber('id');{id} is a [id] segment, and the int annotation is the constraint: /api/users/abc answers
404.
async def GET(id: int): return {"id": id}An optional {slug?} or a {path} matching slashes (->where('path', '.*')) is a
[...path].py catch-all.
Controllers
class NoteController extends Controller{ public function show(int $id) { return ['id' => $id]; } public function destroy(int $id) { return ['deleted' => $id]; }}Route::get('/notes/{id}', [NoteController::class, 'show']);Route::delete('/notes/{id}', [NoteController::class, 'destroy']);The controller methods are the functions of the file, named after the verb:
async def GET(id: int): return {"id": id} async def DELETE(id: int): return {"deleted": id}For classes with injected services, as in a Laravel controller, see the modular router.
Query strings
public function __invoke(Request $request){ return ['q' => $request->query('q', ''), 'page' => (int) $request->query('page', 1)];}async def GET(q: str = "", page: int = 1): return {"q": q, "page": page}Validation and Form Requests
public function store(Request $request){ $data = $request->validate([ 'name' => 'required|string|max:100', 'price' => 'required|numeric|gt:0', ]); return response()->json($data, 201);}The rules are a Pydantic model. A body that breaks one answers 422 with the field and the rule,
as Laravel does:
from pydantic import BaseModel, Field from nexy.http import JSONResponse class NewItem(BaseModel): name: str = Field(max_length=100) price: float = Field(gt=0) async def POST(item: NewItem): return JSONResponse(item.model_dump(), status_code=201)Route groups with a prefix and middleware
Route::prefix('admin')->middleware('admin')->group(function () { Route::get('/stats', fn () => ['users' => 3]);});public function handle(Request $request, Closure $next){ abort_unless($request->header('X-Role') === 'admin', 403, 'Admins only'); return $next($request);}The folder is the prefix, and its dependencies.py is the middleware of the group:
async def GET(): return {"users": 3}from nexy.http import Header, HTTPException def require_admin(x_role: str = Header("")) -> None: if x_role != "admin": raise HTTPException(status_code=403, detail="Admins only") dependencies = [require_admin]Global middleware
public function handle(Request $request, Closure $next){ $response = $next($request); $response->headers->set('X-App', 'demo'); return $response;}A middleware.py at the routes root wraps every request:
from nexy.http import Request async def middleware(request: Request, call_next): response = await call_next(request) response.headers["x-app"] = "demo" return responseBlade views
Route::get('/users', fn () => view('users', ['users' => [['name' => 'Ada'], ['name' => 'Alan']]]));@extends('layouts.app') @section('content')<ul> @foreach ($users as $user) <li>{{ $user['name'] }}</li> @endforeach</ul>@endsectionThe route and the view are one .nexy file:
users = [{"name": "Ada"}, {"name": "Alan"}]<ul> {% for user in users %} <li>{{ user.name }}</li> {% endfor %}</ul>| Blade | Jinja2 in .nexy |
|---|---|
{{ $name }} (escaped) |
{{ name | e }}: .nexy does not escape by default |
{!! $html !!} |
{{ html | safe }} |
@if ($user) ... @endif |
{% if user %} ... {% endif %} |
@foreach ($items as $item) |
{% for item in items %} |
@php ... @endphp |
Python in the frontmatter |
Layouts and @yield
<!DOCTYPE html><html> <body>@yield('content')</body></html>A layout.nexy wraps every page of its folder, with no @extends in the pages:
children: prop[NexyElement]<!DOCTYPE html><html> <body>{{ children | safe }}</body></html>Blade components
@props(['title'])<article> <h2>{{ $title }}</h2> {{ $slot }}</article><x-card title="Latest"><p>This week</p></x-card>
title: prop[str]<article> <h2>{{ title }}</h2> <Slot /></article>from "@/components/Card.nexy" import Card<Card title="Latest"> <p>This week</p></Card>@ is src/: nx new writes useAliases = {"@": "src"} in nexyconfig.py.
Forms, @csrf and redirects
<form method="POST" action="{{ route('notes.store') }}"> @csrf <input name="title"> <button type="submit">Add</button></form>public function store(Request $request){ Note::create($request->validate(['title' => 'required'])); return back();}An action is the controller method the form posts to. Its parameters are the fields, and the
visitor goes back() with a 303 on success:
from nexy.decorators import Action NOTES: list[str] = [] @Actionasync def add_note(title: str) -> None: NOTES.append(title)from src.actions.notes import NOTES<form method="post" action="{{ __actions_path['add_note'] }}"> <input name="title"> <button type="submit">Add</button></form><ul> {% for note in NOTES %} <li>{{ note }}</li> {% endfor %}</ul>Eloquent and migrations
class Post extends Model{ protected $fillable = ['title', 'published'];} $posts = Post::where('published', true)->get();Nexy has no ORM of its own. nx new can scaffold SQLModel, SQLAlchemy or Tortoise-ORM; with
SQLModel:
from sqlmodel import Field, SQLModel class Post(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) title: str published: bool = Falsefrom sqlmodel import Session, select from src.configs.db import enginefrom src.models.post_model import Post async def GET(): with Session(engine) as session: posts = session.exec(select(Post).where(Post.published)).all() return {"titles": [post.title for post in posts]}| Artisan | Nexy |
|---|---|
php artisan make:migration |
nx migrate -m "add posts" |
php artisan migrate |
nx migrate --upgrade |
See Databases.
abort(404) and error views
Route::get('/posts/{slug}', function (string $slug) { abort_unless(isset(POSTS[$slug]), 404); return view('post', ['post' => POSTS[$slug]]);});notFound() is abort(404), and notfound.nexy is resources/views/errors/404.blade.php:
from nexy import notFound POSTS = {"hello": "Hello, world"} slug: prop[str]if slug not in POSTS: notFound()<h1>{{ POSTS[slug] }}</h1><h1>Not found</h1>Sessions
public function store(Request $request){ session(['user' => $request->input('name')]); return ['user' => session('user')];}from nexy.core.models import NexyConfigModel class NexyConfig(NexyConfigModel): useAliases = {"@": "src"} useSession = {"secret_key": "change-me"}from pydantic import BaseModel from nexy.http import Request class Login(BaseModel): name: str async def POST(request: Request, body: Login): request.session["user"] = body.name return {"user": body.name}A page reads the session with useSession().
Queues and dispatch
SendWelcomeEmail::dispatch($user); return response()->json(['queued' => true], 202);For work that can run after the answer, FastAPI's background tasks are enough:
from nexy.http import BackgroundTasks, JSONResponse SENT: list[str] = [] def send_welcome_email(email: str) -> None: SENT.append(email) async def POST(email: str, tasks: BackgroundTasks): tasks.add_task(send_welcome_email, email) return JSONResponse({"queued": True}, status_code=202)For retries and workers on other machines, use a Python queue such as Celery, RQ or arq.
Caching a page
Route::get('/blog', BlogController::class)->middleware('cache.headers:public;max_age=60');
revalidate = 60<h1>Blog</h1>The page is cached per URL and rendered again in the background after 60 seconds.
.env and config/
| Laravel | Nexy |
|---|---|
.env + config/database.php |
.env with DATABASE_URL, read by src/configs/db.py |
config/cors.php |
useCORS in nexyconfig.py |
APP_KEY for sessions |
useSession = {"secret_key": ...} |
public/ |
public/, served at / |
Livewire and Inertia
Livewire's server-driven components map to actions called from a page. Inertia's React or Vue
pages map to .tsx or .vue routes, or to islands inside a .nexy page. See
Actions and Islands.
Tests
public function test_hello(): void{ $this->getJson('/api/hello')->assertExactJson(['message' => 'Hello']);}from fastapi.testclient import TestClient from nexy.app import app def test_hello(): assert TestClient(app).get("/api/hello").json() == {"message": "Hello"}Running
| Laravel | Nexy |
|---|---|
laravel new |
nx new |
php artisan serve |
nx dev |
php artisan route:list |
/docs |
php artisan test |
nx test |
npm run build |
nx build |