VueJS integration for PHP frameworks
PHP-VueJS adds VueJS to any PHP projects, it could be native, or even PHP Framework projects
The recommended way to use this library is using Composer, run this command from the directory where is located composer.json
composer require phpmv/php-vuejsIt manages the whole script injected , you must instantiate it
$vueManager = VueManager::getInstance();VueManager methods
$vue = new VueJs();VueJS arguments
VueJS methods
$component = new VueJSComponent('component-one');VueJSComponent argument's
Component name must respect kebab-case principle, if you don't provide a variable name, it would be name converted to PascalCase
VueJSComponent methods
$object->addData('name',true);
$object->addDataRaw('name','true');addData, addDataRaw arguments
These two lines of code generate exactly the same Vue data
data: { "name": true }$object->addMethod('greet','window.alert(`Hello ${name}`);',['name']);addMethod arguments
This will generate the content below
methods: {
"greet":
function(name){
window.alert(`Hello ${name}`);
}
}$object->addComputed('reversedMessage',"return this.message.split('').reverse().join('');");addComputed arguments
This will generate the content below
computeds: {
reversedMessage:
function(){
return this.message.split('').reverse().join('');
}
}Here is an example with getter and setter
$object->addComputed(
'fullName',
"return this.firstName+' '+this.lastName",
"this.firstName=v[0];this.lastName=v[1]");This code generates this content
computeds: {
fullName: {
get: function(){
return this.firstName+' '+this.lastName
},
set: function(v){
this.firstName=v[0];
this.lastName=v[1]
}
}
}$object->addWatcher(
"title",
"console.log('Title change from '+ oldTitle +' to '+ newTitle)",
['newTitle','oldTitle']);addWatcher arguments
This generates the content below
watch: {
"title":
function(newTitle,oldTitle){
console.log('Title change from '+ oldTitle +' to '+ newTitle)
}
}These are all the methods available to run a function at a given lifecycle
All hooks work in the same way, the underneath example can be applied for each hooks
hooks arguments
$object->onCreated("console.log('Page has been created !');");This generates the content below
created:
function(){
console.log('Page has been created !');
}addDirective, addGlobalDirective arguments
$object->addDirective('focus',['inserted'=>"el.focus()"]);This generates the content below
directives: {
focus: {
inserted:
function(el,binding,vnode,oldVnode){
el.focus()
}
}
}$vueManager->addGlobalDirective('focus',['inserted'=>"el.focus()"]);This generates the content below
Vue.directive('focus',{
inserted:
function(el,binding,vnode,oldVnode){
el.focus()
}
});addFilter, addGlobalFilter arguments
$object->addFilter(
'capitalize',
"if(!value){"
."return '';"
."}"
."value = value.toString();"
."return value.charAt(0).toUpperCase() + value.slice(1);",
["value"]);This generates the content below
filters: {
capitalize: function(value){
if(!value){return '';}
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1);
}
}$vueManager->addGlobalFilter(
'capitalize',
"if(!value){"
."return '';"
."}"
."value = value.toString();"
."return value.charAt(0).toUpperCase() + value.slice(1);",
["value"]);This generates the content below
Vue.filter('capitalize',
function(value){
if(!value){return '';}
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1);
});addComponent, addGlobalComponent, importComponentObject arguments
$component = new VueJSComponent('component-one');
$component->addData('framework','ubiquity');
$vue->addComponent($component);This generates the content below
components: { "component-one": ComponentOne }$component = new VueJSComponent('component-one');
$component->addData('framework','ubiquity');
$vueManager->importComponentObject($component);This generates the content below
const ComponentOne = {
data:
function(){
return {framework: "ubiquity"}
}
};$component = new VueJSComponent('component-one');
$component->addData('framework','ubiquity');
$vueManager->addGlobalComponent($component);This generates the content below
Vue.component('component-one',{
data:
function(){
return {framework: "ubiquity"}
}
});addMixin, addGlobalMixin argument
$mixin = new VueJSComponent('mixin-one');
$mixin->addData('framework','ubiquity');
$vue->addMixin($mixin);This generates the content below
mixins: [ MixinOne ]$mixin = new VueJSComponent('mixin-one');
$mixin->addData('framework','ubiquity');
$vueManager->addGlobalMixin($mixin);This generates the content below
Vue.mixin({
data:
function(){
return {framework: "ubiquity"}
}
});addGlobalExtend, extends argument
$component = new VueJSComponent('component');
$componentOne = new VueJSComponent('component-one');
$componentOne->addData('framework','ubiquity');
$componentOne->extends($component);
$vueManager->addGlobalComponent($componentOne);This generates the content below
extends: "Component"$extend = new VueJSComponent('extend-one');
$extend->addData('framework','ubiquity');
$vueManager->addGlobalMixin($extend);This generates the content below
Vue.extend({
data:
function(){
return {framework: "ubiquity"}
}
});addGlobalObservable arguments
$vueManager->addGlobalObservable("state", ["count" => 0]);This generates the content below
const state = Vue.observable({count: 0});To enable axios
$vueManager->setAxios(true);To enable setInheritAttrs
$component->setInheritAttrs(true);setModel arguments
$component->setModel('checked', 'change');addVue argument
$vueManager->addVue($vue);