Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
ianrothmann1 committed Jul 29, 2017
1 parent 3c4185b commit f9c6e88
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ composer require ianrothmann/laravel-vue-bridge
In config/app.php

Service provider
```php
```
IanRothmann\LaravelVueBridge\ServiceProviders\VueBridgeServiceProvider::class
```
Facade
```php
```
'VueBridge' =>IanRothmann\LaravelVueBridge\Facades\VueBridge::class
```
In your main blade file, before
```javascript
```
<script src="{{ mix('js/app.js') }}"></script>
```
you should add
```php
```
{!! VueBridge::scripts(get_defined_vars()) !!}
```
# Exposing variables
In your controller:
```php
```
VueBridge::exposeVariables([array of variable names to expose]);
VueBridge::hideVariables([array of variable names to hide, all others will be exposed]);
Expand All @@ -38,15 +38,12 @@ VueBridge::hideAllVariables();

# Exposing Routes
In web.php or in middleware. Routes are hidden by default:
```php
```
VueBridge::exposeAllRoutes(); //exposes all named routes
VueBridge::exposeRoutes(([array of route names]);
VueBridge::hideRoutes([array of route names]);
```

# View helper
Replace return `view(...)` in your controllers with `return vbview(...)`. You can then create a viewMixin in `resources/js/views/`.

The file structure inside `resources/js/views/` should match `resources/views/` exactly. for instance `return vbview('profile.update',compact('user'))` will return the `resources/views/profile/update.blade.php` view including the viewMixin `resources/js/views/profile/update.js`. It will also expose the `user` variable in Vuex.
More documentation to follow later
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "ianrothmann/laravel-vue-bridge",
"description": "A bridge to expose Laravel variables and routes in Vue and Vuex",
"version": "0.5.0",
"type": "library",
"license": "Apache2",
"authors": [
Expand All @@ -12,8 +11,7 @@
],
"require": {
"php": ">=5.4.0",
"laravel/framework": "5.4.*",
"lavary/laravel-menu": "^1.6"
"laravel/framework": "5.4.*"
},
"autoload": {
"psr-4": {
Expand Down
35 changes: 35 additions & 0 deletions src/ExposeClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: ian
* Date: 2017/04/12
* Time: 10:21 AM
*/

namespace IanRothmann\LaravelVueBridge;


use Illuminate\Support\Facades\Route;

trait ExposeClosure
{

private $closures=[];

public function exposeClosureResult($varName,$closure){
if($closure instanceof \Closure) {
$this->closures[$varName] = $closure;
}
}

private function getClosureVariables(){

$data=[];
foreach ($this->closures as $varName=>$closure){
$data[$varName]=$closure();
}

return $data;
}

}
33 changes: 26 additions & 7 deletions src/LaravelVueBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ class LaravelVueBridge
{
use ExposeRoutes;
use ExposeVariables;
use ExposeClosure;

private $pagescripts=[];
private $viewJsPath='js/views/';

public function __construct()
{

}

public function test(){
dd('HALo d');
}

function view($view,$data=[],$mergeData=[]){
$this->exposeVariables($data)
->pageScript(str_replace('.','/',$view).'.js');
Expand All @@ -38,11 +36,32 @@ public function pageScript($scriptpath){
return $this;
}

/**
* @return string
*/
public function getViewJsPath()
{
return $this->viewJsPath;
}

/**
* @param string $viewJsPath
* @return LaravelVueBridge
*/
public function setViewJsPath($viewJsPath)
{
$this->viewJsPath = $viewJsPath;
return $this;
}




public function scripts($defined_vars){
$export_routes=json_encode($this->getExposedRoutes());
$export_vars=json_encode($this->getExposedVariables($defined_vars));
$export_vars=json_encode(array_merge($this->getExposedVariables($defined_vars),$this->getClosureVariables()));

// print_r($export_vars);die();
$session_status='';
if(session('error')){
$session_status='const sessionStatus={message:"'.str_replace('"','\"',session('error')).'",messagetype:"error"};';
Expand All @@ -55,13 +74,13 @@ public function scripts($defined_vars){
$js=<<<TOC
<script type="text/javascript">
const routeActions = $export_routes;
const pageData=$export_vars;
const serverData=$export_vars;
$session_status
</script>
TOC;

foreach ($this->pagescripts as $script) {
$js.='<script src="'.mix('js/pages/'.$script).'"></script>';
$js.='<script src="'.mix($this->viewJsPath.$script).'"></script>';
}

return $js;
Expand Down
11 changes: 11 additions & 0 deletions src/ServiceProviders/VueBridgeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ class VueBridgeServiceProvider extends ServiceProvider
{
public function register(){
$this->app->bind('vue-bridge','IanRothmann\LaravelVueBridge\LaravelVueBridge');
$this->registerHelpers();
}

/**
* Register helpers file
*/
public function registerHelpers()
{
if (file_exists( __DIR__.DIRECTORY_SEPARATOR.'../helpers.php'))
{
require __DIR__.DIRECTORY_SEPARATOR.'../helpers.php';
}
}


Expand Down
5 changes: 5 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

function vbview($view, $data = array(), $mergeData = array()){
return VueBridge::view($view, $data, $mergeData);
}

0 comments on commit f9c6e88

Please sign in to comment.