PHP code example of rzl-zone / ziggy-route

1. Go to this page and download the library: Download rzl-zone/ziggy-route 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/ */

    

rzl-zone / ziggy-route example snippets


Route::get("posts", fn (Request $request) => /* ... */)->name("posts.index");

Route::get("posts/{post}", fn (Post $post) => /* ... */)->name("posts.show");

Route::get("venues/{venue}/events/{event}", fn (Venue $venue, Event $event) => /* ... */)->name("venues.events.show");

Route::get("venues/{venue}/events/{event}", fn (Venue $venue, Event $event) => /* ... */)->name("venues.events.show");

// app/Models/Post.php

class Post extends Model
{
    public function getRouteKeyName()
    {
        return "slug";
    }
}

Route::get("blog/{post}", function (Post $post) {
    return view("posts.show", ["post" => $post]);
})->name("posts.show");

Route::get("authors/{author}/photos/{photo:uuid}", fn (Author $author, Photo $photo) => /* ... */)
    ->name("authors.photos.show");

namespace App\Http\Middleware;

use Inertia\Middleware;
use Illuminate\Http\Request;
use RzlZone\ZiggyRoute\RzlZiggy;
use Illuminate\Foundation\Inspiring;

class HandleInertiaRequests extends Middleware
{ 
  // modify at share method only.
  public function share(Request $request): array
  {  
    return [
      ...parent::share($request), 
      
      //! This is ing `@rzlRoutes` directive
      "rzlZiggy" => fn(): array => [
        ...(new RzlZiggy())->jsonConfigsSsr(), 
      ],
      
      // ....other your props config
    ];
  }
}


// config/rzl-ziggy.php

return [
    "only" => ["home", "posts.index", "posts.show"],

    // other you config...
];

// config/rzl-ziggy.php

return [
    "except" => ["_debugbar.*", "horizon.*", "admin.*"],

    // other you config...
];

// config/rzl-ziggy.php

return [
    "groups" => [
        "admin" => ["admin.*", "users.*"],
        "author" => ["posts.*"],
    ],

    // other you config...
];

Route::get("{locale}/posts/{post}", fn (Post $post) => /* ... */)->name("posts.show");

// config/rzl-ziggy.php

return [
    /** Default values for dynamic route parameters.
     *
     * These values will be applied automatically to any route parameter
     * that exists in the route URI but is not explicitly passed during route generation.
     *
     * Useful for things like localization (e.g. `{locale}`), tenant identifiers (e.g. `{team}`),
     * or filtering (e.g. `{type}`) without having to always specify them in the frontend.
     *
     * You can override these at runtime via `URL::defaults([...])`.
     */
    "defaults" => [
        /** Default for `{locale}` route parameter. CLI `--locale` overrides this value. */
        "locale" => env("APP_LOCALE", "en"),

        // another example:...
        // "type"   => "default", // Default value for routes requiring {type}
        // "team"   => "main",    // Default tenant/team identifier (e.g. {team})
    ],

    // other you config...
];

// config/rzl-ziggy.php

return [
  "lang" => "ts",

  // other you config...
];

// config/rzl-ziggy.php

return [
  "output" => [
    "name" => "index", // ➔ index.ts or index.js

    "path" => [
      "main" => "resources/js/rzl-ziggy/routes", // Output folder only
    ]
  ],
  
  // other you config...
];

@rzlRoutes(nonce: "your-nonce-here")

// routes/api.php

use RzlZone\Ziggy\RzlZiggy;

Route::get("rzl-ziggy", fn () => response()->json(new RzlZiggy));
js
route("venues.events.show", {
    venue: 1,
    event: 2,
    page: 5,
    count: 10,
});
// ➔ "/venues/1/events/2?page=5&count=10"
route("venues.events.show", {
    venue: 1,
    event: 2,
    page: 5,
    count: 10,
    type: "active"
}, true);
// ➔ "https://rzl.test/venues/1/events/2?page=5&count=10&type=active"
js
route("posts.show", { post: 5, type: "active" });     
// ➔ "/de/posts/5?type=active"
route("posts.show", { post: 1 }, true);               
// ➔ "https://rzl.test/de/posts/1"
route("posts.show", { post: 1, locale:"ar" });        
// ➔ "/ar/posts/1"
route("posts.show", { post: 1, locale:"en" }, true);  
// ➔ "https://rzl.test/en/posts/1"
bash
php artisan rzl-ziggy:generate --types
ts
import { route as routeFn } from "@rzl-zone/ziggy-route";

declare global {
    var route: typeof routeFn;
}
bash
php artisan rzl-ziggy:generate
tsx
import {
  route as defaultRoute,
  type Config,
  type RouteName,
  type RouteParams
} from "@rzl-zone/ziggy-route";

createServer((page) =>
  createInertiaApp({
    setup: ({ App, props }) => {
      //! this is params,
          absolute,
          page.props.rzlZiggy as Config
        );
      };

      return <App {...props} />;
    },
    // ....other your props config
  })
);
bash
php artisan vendor:publish --tag=rzl-ziggy
blade
{{-- authors.blade.php --}}

@rzlRoutes("author")
blade
{{-- admin.blade.php --}}

@rzlRoutes(["admin", "author"])
bash
php artisan rzl-ziggy:generate
# or
php artisan rzl-ziggy:generate --types