PHP code example of fusionphp / fusion

1. Go to this page and download the library: Download fusionphp/fusion 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/ */

    

fusionphp / fusion example snippets


<php>
  new class {
    public string $name;

    public function mount()
    {
      $this->name = Auth::user()->name;
    }
  }
</php>

<template>
  Hello {{ name }}!
</template>

// web.php
use Fusion\Fusion;

Fusion::pages();

// web.php
use Fusion\Fusion;

// All pages in resources/js/Pages/Marketing end up at the root.
Fusion::pages(root: '/', directory: 'Marketing');

// web.php
use Fusion\Fusion;

// All marketing pages end up at the root.
Fusion::pages(root: '/', directory: 'Marketing');

// All files in `Cases` end up at `/case-studies`
Fusion::pages(root: '/case-studies', directory: 'Cases');

$podcast = prop()->fromRoute();

// Variable is named $pod, but the route parameter is
// `podcast` to match the filename of [Podcast].vue.
$pod = prop()->fromRoute('podcast');

new class {
    public function mount($podcast)
    {
        // Do something with $podcast! Usually that
        // means setting it to a public property.
    }
}

new class {
    // Will be set to the route parameter.
    public $podcast;
}

$podcast = prop()->fromRoute(class: \App\Models\Podcast::class);

new class {
    // Merely hint the type as a UrlRoutable. 
    public function mount(\App\Models\Podcast $podcast)
    {
        // Podcast is now an Eloquent Model.
    }
}

new class {
    public \App\Models\Podcast $podcast;
}

$podcast = prop()->fromRoute(
    class: \App\Models\Podcast::class,
    // Route by a custom key instead of `id`
    using: 'slug',
    // Include soft-deleted models. 
    withTrashed: true
);

$wild = prop()->fromRoute();
// $wild === ['the-best', 'show-in-the-world', 'a8f74b']

// Get the unique ID from the parts.
$id = last($wild);

new class {
    public function mount(array $wild) 
    {
        $id = last($wild);   
    }
}

use Fusion\Fusion;
use Symfony\Component\Finder\Finder;

Fusion::pages('/', fn() => (new Finder)
    // Start in any directory you please.
    ->in(config('fusion.paths.pages'))
    // Some file patterns you don't want routed.
    ->notName('*.template.vue')
    // Maybe we route these separately? Who knows.
    ->exclude([
        'Cases',
        'Marketing'
    ])
);

// web.php
use Fusion\Fusion;

Fusion::page(uri: '/hello-world', component: 'Custom/HelloWorld');

use function \Fusion\prop;

$podcast = prop();

new class 
{
    //
}

new class extends \Fusion\FusionPage
{
    //
}

return new class
{
    //
}

$name = prop();

$name = prop('Aaron');
// Or
$name = prop(fn() => 'Aaron');

$name = prop('Aaron');

$name = strtoupper($name);

// "AARON" gets sent to the frontend.

$name = prop(Auth::user()->name)->readonly();

$podcasts = prop(fn() => Podcast::all())->readonly();

$search = prop()->syncQueryString();

$search = prop()->syncQueryString(as: 's');

new class 
{
    public string $name = "Aaron";
}

new class 
{
    public string $name = "Aaron";
}

new class 
{
    #[\Fusion\Attributes\IsReadOnly]
    public string $name = "Aaron";
}

new class 
{
    public string $name;
    
    public function mount() 
    {
        // Doesn't matter what the frontend sends,
        // we'll just overwrite it.
        $this->name = 'Aaron';
    }
}

new class 
{
    public ?string $name = null;
    
    public function mount() 
    {
        // Set it to Aaron, only if it's not set.
        $this->name ??= 'Aaron';
    }
}

new class 
{
    #[\Fusion\Attributes\SyncQueryString]
    public string $search = '';
}

new class 
{
    #[\Fusion\Attributes\SyncQueryString(as: 's')]
    public string $search = '';
}

new class 
{
    public function hello() 
    {
        // Do something
    }
}

new class 
{
    #[\Fusion\Attributes\ServerOnly]
    public function hello() 
    {
        // This method is not addressable from the frontend.
    }
}

expose(hello: function() {
    // This is available on the frontend as `hello`.
})

expose(favorite: function() {
    // This is available on the frontend.
})

new class {
    public function favorite()
    {
        // This is available.
    }
}
vue
<php>
  // Define a prop in PHP
  $name = prop(Auth::user()->name);
</php>

<template>
  <!-- Use it in Vue! -->
  Hello {{ name }}!
</template>
bash
php artisan fusion:install