tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.91k stars 248 forks source link

Ziggy ignores web.php routes #695

Closed overclocked555 closed 10 months ago

overclocked555 commented 10 months ago

Ziggy version

v1.8.1

Laravel version

v10.34.2

Description

Ziggy ignores web.php routes in my project The command php artisan ziggy:generate does not create routes contactsand products: in resources\js\ziggy.js I also ran these commands:

$ php artisan route:clear

   INFO  Route cache cleared successfully.
$ php artisan route:list

  GET|HEAD   / .................................................................................................................................................. 
  POST       _ignition/execute-solution ........................................... ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController
  GET|HEAD   _ignition/health-check ....................................................... ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController
  POST       _ignition/update-config .................................................... ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController
  GET|HEAD   contacts ........................................................................................................................................... 
  GET|HEAD   products ........................................................................................................................................... 
  GET|HEAD   sanctum/csrf-cookie .............................................................. sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show

                                                                                                                                               Showing [7] routes

The project also has the following packages installed:

    "require": {
        "php": "^8.1",
        "guzzlehttp/guzzle": "^7.2",
        "inertiajs/inertia-laravel": "^0.6.11",
        "laravel/framework": "^10.10",
        "laravel/sanctum": "^3.3",
        "laravel/tinker": "^2.8",
        "laravel/ui": "^4.2",
        "mcamara/laravel-localization": "^1.8",
        "spatie/laravel-translatable": "^6.5",
        "spatie/laravel-translation-loader": "^2.7",
        "tightenco/ziggy": "^1.8"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.18",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.1",
        "spatie/laravel-ignition": "^2.0"
    },

Ziggy call and context

// console
$ php artisan ziggy:generate
Files generated!

Ziggy configuration

//console.log(Ziggy)
{
    "url": "http://iwtm",
    "port": null,
    "defaults": {},
    "routes": {
        "sanctum.csrf-cookie": {
            "uri": "sanctum/csrf-cookie",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "ignition.healthCheck": {
            "uri": "_ignition/health-check",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "ignition.executeSolution": {
            "uri": "_ignition/execute-solution",
            "methods": [
                "POST"
            ]
        },
        "ignition.updateConfig": {
            "uri": "_ignition/update-config",
            "methods": [
                "POST"
            ]
        }
    }
}

// resources\js\ziggy.js
const Ziggy = {"url":"http:\/\/iwtm","port":null,"defaults":{},"routes":{"sanctum.csrf-cookie":{"uri":"sanctum\/csrf-cookie","methods":["GET","HEAD"]},"ignition.healthCheck":{"uri":"_ignition\/health-check","methods":["GET","HEAD"]},"ignition.executeSolution":{"uri":"_ignition\/execute-solution","methods":["POST"]},"ignition.updateConfig":{"uri":"_ignition\/update-config","methods":["POST"]}}};

if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') {
    Object.assign(Ziggy.routes, window.Ziggy.routes);
}

export { Ziggy };

Route definition

// routes\web.php
<?php

use App\Models\Product;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::get('/', fn () => Inertia::render('Home'));
Route::get('/contacts', fn () => Inertia::render('Contacts'));
Route::get('/products', fn () => Inertia::render('Products', ['products' => Product::all()]));
bakerkretzmar commented 10 months ago

Ziggy (like Laravel's route() function) deals with named routes. Routes without names will be ignored. Adding ->name('home'), ->name('contacts'), and ->name('products') to those routes will fix this for you.