Installation:
composer config extra.symfony.allow-contrib true
composer require disjfa/mozaic-bundle
Ensure your composer.json includes the bundle in the extra.bundles section.
Environment Setup:
Add Unsplash API credentials to .env:
UNSPLASH_APPLICATION_ID=your_id
UNSPLASH_APPLICATION_SECRET=your_secret
Basic Usage:
import { MozaicComponent } from 'mozaic-bundle';
new Vue({
el: '#app',
components: { MozaicComponent }
});
webpack.config.js is configured to handle Encore and Vue.First Use Case: Use the bundle to fetch and display Unsplash images dynamically in a Vue component. Example:
<mozaic-component :query="'nature'" :count="10"></mozaic-component>
Vue Component Integration:
MozaicComponent to customize behavior (e.g., filtering, pagination).// CustomComponent.vue
export default {
extends: MozaicComponent,
props: ['customFilter'],
methods: {
fetchImages() {
this.$parent.fetchImages(this.customFilter);
}
}
};
API Interaction:
fetchImages() to inject custom logic:
methods: {
async fetchImages(query) {
const response = await this.$http.get(`/api/images?query=${query}`);
this.images = response.data;
}
}
Symfony Integration:
<div id="app" data-images="{{ images|json_encode }}"></div>
mounted() lifecycle hook:
mounted() {
this.images = JSON.parse(this.$el.dataset.images);
}
Webpack Encore:
webpack.config.js includes:
Encore
.enableVueLoader(() => {}, { version: 3 })
.addEntry('app', './assets/js/app.js')
.splitEntry('./assets/js/mozaic.js')
.enableSingleRuntimeChunk();
MozaicComponent only when needed:
if (document.getElementById('mozaic-container')) {
import('./mozaic-component').then(component => {
new Vue({ components: { MozaicComponent: component.MozaicComponent } }).$mount('#mozaic-container');
});
}
// store.js
export default new Vuex.Store({
state: { images: [] },
mutations: { SET_IMAGES(state, images) { state.images = images; } }
});
// test.js
const mockImages = [{ id: 1, urls: { raw: 'mock-url' } }];
global.$http = { get: jest.fn().mockResolvedValue({ data: mockImages }) };
Recipe Dependency:
symfony/recipes-contrib. If recipes fail, manually configure:
# config/packages/mozaic.yaml
mozaic:
unsplash:
enabled: true
composer.json has:
"extra": {
"symfony": {
"allow-contrib": true
}
}
Webpack Encore Compatibility:
node_modules are installed (npm install).public/build/ for compiled assets).yarn build --watch
API Rate Limits:
# config/packages/cache.yaml
framework:
cache:
app: cache.adapter.redis
Vue 2 vs. Vue 3:
webpack.config.js:
resolve: {
alias: {
vue$: 'vue/dist/vue.esm-bundler.js'
}
}
Console Errors:
403 (API errors) or 404 (missing assets). Validate .env and Unsplash credentials.console.log(this.$options) in components to inspect props/methods.Symfony Logs:
APP_DEBUG=1) and check var/log/dev.log for API request failures.Custom API Endpoint: Override the Unsplash service in Symfony:
# config/services.yaml
services:
App\Service\CustomUnsplashService:
arguments:
$clientId: '%env(UNSPLASH_APPLICATION_ID)%'
$clientSecret: '%env(UNSPLASH_APPLICATION_SECRET)%'
Bind it in the bundle’s configuration.
Component Slots: Extend the Vue component to support slots:
// MozaicComponent.vue
template: `
<div>
<slot name="header"></slot>
<div v-for="image in images" :key="image.id">
<img :src="image.urls.raw" />
<slot name="footer" :image="image"></slot>
</div>
</div>
`;
Server-Side Rendering (SSR):
Use Symfony’s Twig to pre-render components:
<div id="app" data-images="{{ images|json_encode }}">
{% include 'mozaic/_component.html.twig' %}
</div>
Hydrate with Vue in JavaScript:
new Vue({
el: '#app',
data() { return { images: JSON.parse(this.$el.dataset.images) } }
});
Legacy Support:
For Symfony 3.4, pin dependencies in composer.json:
"require": {
"php": "~5.5.9|~7.0",
"symfony/symfony": "3.4.*"
}
How can I help you explore Laravel packages today?