


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
- addGlobalDirective
- addGlobalFilter
- addGlobalExtend
- addGlobalMixin
- addGlobalObservable
- addGlobalComponent
- importComponentObject
- setAxios
- addVue
$vue = new VueJs();VueJS arguments
- (associative array) configuration = ["el" => "#app"] [optionnal]
- (string) variable name = "app" [optionnal]
- (boolean) enable vuetify = false [optionnal]
VueJS methods
- addData
- addDataRaw
- addMethod
- addComputed
- addWatcher
- addDirective
- addFilter
- addComponent
- addMixin
- addHook
$component = new VueJSComponent('component-one');VueJSComponent argument's
- (string) name
- (string) variable name = null [optionnal]
Component name must respect kebab-case principle, if you don't provide a variable name, it would be name converted to PascalCase
VueJSComponent methods
- addProps
- setInheritAttrs
- setModel
- addData
- extends
- addDataRaw
- addMethod
- addComputed
- addWatcher
- addDirective
- addFilter
- addComponent
- addMixin
- addTemplate
- generateFile
- addHook
$object->addData('name',true);
$object->addDataRaw('name','true');addData, addDataRaw arguments
- (string) name
- (string) value
These two lines of code generate exactly the same Vue data
data: { "name": true }$object->addMethod('greet','window.alert(`Hello ${name}`);',['name']);addMethod arguments
- (string) name
- (string) function's body
- (array) function's argument(s) [optionnal]
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
- (string) name
- (string) get function's body
- (string) set function's body by default the function argument is v [optionnal]
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
- (string) data to watch
- (string) function body
- (array) function argument(s) [optionnal]
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
- onBeforeCreate
- onCreated
- onBeforeMount
- onMounted
- onBeforeUpdate
- onUpdated
- onBeforeDestroy
- onDestroyed
- onActivated
- onDeactivated
All hooks work in the same way, the underneath example can be applied for each hooks
hooks arguments
- (string) function's body
$object->onCreated("console.log('Page has been created !');");This generates the content below
created:
function(){
console.log('Page has been created !');
}addDirective, addGlobalDirective arguments
- (string) directive's name
- (associative array) [hook => hook's function]
$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
- (string) name
- (string) function body
- (array) function arguments [optionnal]
$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
- (VueJSComponent) component object
$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
- (VueJSComponent) mixin object
$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
- (VueJSComponent) extend object
$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
- (string) variable name
- (array) object
$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
- (string) props
- (string) events
$component->setModel('checked', 'change');addVue argument
- (VueJS) object vue
$vueManager->addVue($vue);