Getting started
How Nitro works
You write a component once, in PHP. Nitro keeps it in two languages: PHP on the server, and JavaScript in the browser, compiled from your PHP. This page follows one component through both: what the compiler makes of it, what the server sends, what happens on a click and on a server call, and how a Vue, React, Solid or Svelte view gets the component.
On this page
One component, two languages
Take a small component: a counter that counts in the browser and saves on the server.
class Counter extends Component
{
public int $count = 0;
/** Runs in the browser: compiled to JavaScript. */
public function increment(): void
{
$this->count++;
}
/** Runs on the server: never compiled. */
#[Server]
public function save(): void
{
Score::create(['value' => $this->count]);
}
}
Behind the scenes, a few parts work together. Their names are used through the rest of this page:
| Part | What it is |
|---|---|
| Component | Your PHP class: its state (the public properties) and its behaviour (the methods) |
| Compiler | Turns the class, and a Blade view, into JavaScript |
| Definition | That JavaScript: the same component, written in JS |
| Runtime |
nitro.js
in the browser: it holds every live component and runs its methods
|
| State | The component's values, signed by the server, travelling between server and browser |
| Adapter | The bridge from a Nitro component to a Vue, React, Solid or Svelte view |
| View |
What the visitor sees: Blade, or a
.vue
,
.jsx
or
.svelte
file
|
PHP and JavaScript never call each other. They share one component in two forms, the PHP class and its compiled definition, and pass its state back and forth. The state is the handshake: whichever side runs next starts from it.
The compiler
Nitro reads each component's class and view and compiles them to JavaScript: the component's
definition. In the counter, increment() becomes JavaScript that does what
$this->count++ does; save() is marked for the server and isn't compiled at all.
The view becomes a tree of elements with its dynamic parts. During development, this happens on the first
request after you change a component. For production, you run php artisan nitro:build to do it
ahead of time.
- PHP's rules still hold. Arrays are values,
==compares the way PHP compares, strings are bytes, and functions behave as PHP's do. All of this is checked against PHP itself. - The browser only gets what it needs. Nitro never compiles a method the browser can't reach, such as a
#[Server]method or a helper that you only call frommount(). - Mistakes fail the build. A browser method that queries the database, or a view the browser can't render the same way, causes an error that names the file and line. You find out during development, not in production.
The first render
The server runs the PHP class and renders the page. The layout is Blade, as Laravel renders any view; a
component's Blade view is the one Nitro compiled, rendered with its expressions as PHP, so the server and
the browser render it by the same rules. The HTML arrives complete, so it appears quickly and search engines and link previews can read it. Along with it
comes each component's state, signed by the server: for the counter,
count: 0.
The browser then takes over the page. The runtime finds each component in the HTML, creates it from its definition and fills it from its state, so the same counter, at 0, is now alive in the browser. It doesn't render the page again: it attaches to the elements the server sent, so nothing moves or flickers.
What happens on a click
nitro:click="increment" calls the compiled increment() method in the browser. No
request is sent: the code PHP wrote runs as JavaScript. The method changes $count, and only the
parts of the view that read $count render again. This works because, when the view was
compiled, every dynamic part recorded which properties it reads:
<div>
<button nitro:click="increment">+</button>
<p>Clicked {{ $count }} times</p> {{-- reads $count --}}
<p>Hello, {{ $name }}</p> {{-- reads $name, so a click leaves it alone --}}
@foreach ($authors as $author) {{-- reads $authors, so neither one changes it --}}
<li>{{ $author }}</li>
@endforeach
</div>
| A change runs | Then compares | |
|---|---|---|
| Livewire | the whole view, on the server | the new HTML against the page |
| React, Vue | the component’s render function | the new virtual DOM against the last one |
| Nitro | the expressions that read the property | nothing: each one updates its own node |
What happens on a server call
A method marked #[Server] runs on the server, in PHP. Each call is a single request, and the
state carries the component across: the server rebuilds the real PHP object from it, runs the method, and
sends the new state back.
-
The browser sends the signed state
It sends the component's state exactly as the server signed it, the properties it has changed since (the counter's
count: 1), and the method with its arguments. -
The server checks everything
- The state is one the server signed, for this session and this user, and it hasn't been changed.
- The page's middleware (
auth,can, ...) still lets the visitor in. - Every changed property is public, isn't
#[Locked], has its declared type, and passes its#[Validate]rules. - The method is a
#[Server]method, and each argument has its parameter's type.
-
The method runs
The server builds the PHP component from the state, with the browser's changes, and runs the method like any other Laravel code, with queries, mail, the session and the container. Models are always read again from the database, never taken from the browser.
-
The browser renders what changed
The response holds the new signed state, along with any events, redirects or messages. The runtime takes it in, and only the parts of the view that read a changed property render again.
The browser can send anything
Anyone can call your endpoint without using the page. An unlocked property can arrive with any value of its type. So check whatever a #[Server] method relies on, with #[Validate] rules or $this->validate().
JavaScript views: the adapter
With a Blade view, the runtime renders the view itself: Nitro compiled it. A Vue, React, Solid or Svelte view is rendered by its framework, so the component has to reach it. That's the adapter's job: it hands the live component to the view in the framework's own way. The component provides; the view receives.
| View | How it gets the component | Reading the count |
|---|---|---|
| Blade | No adapter: the runtime renders the compiled view |
{{ $count }}
|
| Vue | The component's names in the template (refs underneath) |
{{ count }}
|
| React | Its properties and methods, each as a prop |
function Counter({ count, increment })
|
| Solid | Its properties and methods, each as a prop, read where they're used |
props.count
|
| Svelte | Its properties and methods, each as a prop |
let { count, increment } = $props()
|
What a view receives
The adapter spreads the component: each public property, each #[Computed] value and each
method becomes a prop of its own, under its PHP name. For the counter, a React view gets:
function Counter({ count, increment, save, errors, unsaved, set }) { … }
| Prop | Comes from | What it is |
|---|---|---|
count
|
public int $count
|
The value. It updates when the component changes |
increment
|
increment()
|
A function that runs the compiled method in the browser |
save
|
#[Server] save()
|
A function that sends the server call. It also carries
save.processing
and
save.slow
|
errors
,
unsaved
,
uploads
|
Nitro | Validation messages, changes not yet sent, upload progress |
set
,
dispatch
,
link
,
visit
|
Nitro | Write a property, send an event, make a navigate link, visit a page |
So the view never gets the PHP object itself, only those pieces, as if you had written
<Counter count={…} increment={…} save={…} />. Protected and private members never reach
the view, and methods only the server may run arrive as functions that make the request. Vue and Svelte get
the same pieces: Vue as names in the template, Svelte through $props().
It works both ways:
- Component to view. When
countchanges, by a click or a server call, the adapter tells the framework, which updates what reads it. - View to component. An input writes back:
v-modelin Vue,bind:valuein Svelte,set('title', value)in React and Solid. The adapter passes the value to the component, and the runtime sends it with the next server call.
All four adapters share one core: reading values, calling methods, a server call's processing
and slow, errors, uploads. Each adapter only adds its framework's way of updating
the page. That's why one PHP class works with all five views: see the
examples, a blog written five ways.
Navigation
A link with nitro:navigate changes the page without a full reload. Laravel routes the visit
as usual, with its middleware, bindings and redirects, and the browser shows the new page in place of the
old one. The first visit to any URL always gets the server's complete HTML. These docs work this way.
In production
nitro:build compiles every component, minifies it with esbuild, and publishes versioned files
to public/nitro. The web server sends these files without PHP, and browsers cache them for a
year. Each page is in a file of its own, which loads the first time someone visits it. The files hold
views and browser methods, never data. The state travels with each response.
In one sentence
PHP writes the component once. Nitro compiles it to JavaScript so it runs in the browser, keeps PHP for what must stay on the server, passes signed state between the two, and an adapter hands the live component to your chosen framework as its props.