Ziggy

在 JavaScript 中使用 Laravel route。

一、安裝

透過 composer 安裝。
composer require tightenco/ziggy

1. Laravel Blade

在 layout Blade 加上
@routes

2. Vue

@routes 還是要加,並另外加以下程式:
webpack.min.js
const mix = require('laravel-mix');
const path = require('path');

mix
    .alias({
        ziggy: path.resolve('vendor/tightenco/ziggy/dist/vue')
    })
    .vue();
resources/js/app.js
import { createApp } from 'vue';
import { ZiggyVue } from 'ziggy';
window.app = createApp({
    //
})
.use(ZiggyVue)
.mount('#app');

二、設定

config/ziggy.php
<?php

return [
    'only' => [
        'banners.*',
        'shopping.*',
    ],
];

三、使用

幾乎和 php 寫法一樣。
// 'https://ziggy.test/posts'
route('posts.index'); 

// 'https://ziggy.test/posts/1'
route('posts.show', 1);
route('posts.show', [1]);
route('posts.show', { post: 1 });

// 'https://ziggy.test/events/1/venues/2'
route('events.venues.show', [1, 2]);
route('events.venues.show', { event: 1, venue: 2 });
參考文件

留言