Ziggy

在 JavaScript 中使用 Laravel route。

一、安裝

透過 composer 安裝。
composer require tightenco/ziggy

1. Laravel Blade

在 layout Blade 加上
@routes

2. Vue

@routes 還是要加,並另外加以下程式:
webpack.min.js
  1. const mix = require('laravel-mix');
  2. const path = require('path');
  3.  
  4. mix
  5. .alias({
  6. ziggy: path.resolve('vendor/tightenco/ziggy/dist/vue')
  7. })
  8. .vue();
resources/js/app.js
  1. import { createApp } from 'vue';
  2. import { ZiggyVue } from 'ziggy';
  3. window.app = createApp({
  4. //
  5. })
  6. .use(ZiggyVue)
  7. .mount('#app');

二、設定

config/ziggy.php
  1. <?php
  2.  
  3. return [
  4. 'only' => [
  5. 'banners.*',
  6. 'shopping.*',
  7. ],
  8. ];

三、使用

幾乎和 php 寫法一樣。
  1. // 'https://ziggy.test/posts'
  2. route('posts.index');
  3.  
  4. // 'https://ziggy.test/posts/1'
  5. route('posts.show', 1);
  6. route('posts.show', [1]);
  7. route('posts.show', { post: 1 });
  8.  
  9. // 'https://ziggy.test/events/1/venues/2'
  10. route('events.venues.show', [1, 2]);
  11. route('events.venues.show', { event: 1, venue: 2 });
參考文件

留言