Skip to content

Commit 384d64d

Browse files
committed
Remove old first parameter from middleware setup.
Return JSON from your Node server to change the variable name passed to your view.
1 parent ee9a557 commit 384d64d

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,17 @@ In the config file you can change the host and port of the compiler server and c
8686

8787
---
8888

89-
To change the variable name that will contain the HTML and be passed to your view, use the first parameter of the middleware definition.
90-
91-
``` php
92-
Route::get('/', ['middleware' => 'react:layout', 'uses' => 'HomeController@index']);
93-
```
9489

9590
AJAX requests to this route will return the data array passed to your view as plain JSON. So the example above would return:
9691

9792
``` json
9893
{ "title": "Your title here." }
9994
```
10095

101-
To disable the JSON response on AJAX requests, pass "disable_json" to the second parameter.
96+
To disable the JSON response on AJAX requests, pass "disable_json" to the first parameter.
10297

10398
``` php
104-
Route::get('/', ['middleware' => 'react:content,disable_json', 'uses' => 'HomeController@index']);
99+
Route::get('/', ['middleware' => 'react:disable_json', 'uses' => 'HomeController@index']);
105100
```
106101

107102
Merge JSON Response

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "estey/laravel-react-middleware",
3-
"description": "Middleware for React server-side rendering in Laravel.",
3+
"description": "Middleware for React.js server-side rendering in Laravel.",
44
"keywords": ["laravel", "react", "reactjs", "server-side rendering", "middleware"],
55
"homepage": "http://www.bradestey.com/projects/laravel-react-middleware",
66
"license": "MIT",

src/CompileReact.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ public function __construct(Config $config)
5959
*
6060
* @param \Illuminate\Http\Request $request
6161
* @param \Closure $next
62-
* @param string $contentKey
63-
* @param boolean $isAjaxRespondWithJson
62+
* @param string $ajaxRespondsWithJson
6463
* @return mixed
6564
*/
6665
public function handle(
6766
Request $request,
6867
Closure $next,
69-
$contentKey = 'content',
7068
$ajaxRespondsWithJson = null
7169
) {
7270
$this->path = $request->path();
@@ -85,20 +83,19 @@ public function handle(
8583
return $this->respondWithJson();
8684
}
8785

88-
return $this->compile($contentKey);
86+
return $this->compile();
8987
}
9088

9189
/**
9290
* Make an HTTP request to the React compiler. Return the
9391
* compiled view string.
9492
*
95-
* @param string $contentKey
9693
* @return \Illuminate\Http\Response
9794
*/
98-
protected function compile($contentKey)
95+
protected function compile()
9996
{
10097
$content = $this->getResponse();
101-
$contents = !is_object($content) ? [$contentKey => $content] : $content;
98+
$contents = !is_object($content) ? compact('content') : $content;
10299

103100
foreach ($contents as $key => $value) {
104101
$this->view->with($key, $value);

test/Unit/CompileReactTest.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function testHandle()
5858
$mock
5959
->shouldReceive('compile')
6060
->once()
61-
->with('content')
6261
->andReturn('foo bar baz');
6362

6463
$this->assertEquals(
@@ -100,11 +99,10 @@ public function testHandleNoAjax()
10099
$mock
101100
->shouldReceive('compile')
102101
->once()
103-
->with('foo')
104102
->andReturn('foo bar baz');
105103

106104
$this->assertEquals(
107-
$mock->handle($request, $next, 'foo', 'disable_json'),
105+
$mock->handle($request, $next, 'disable_json'),
108106
'foo bar baz'
109107
);
110108
}
@@ -192,9 +190,12 @@ public function testHandleException()
192190

193191
/**
194192
* Helper method for testing the compile() method.
195-
* @return [type] [description]
193+
*
194+
* @param $shouldRespond string
195+
* @param $shouldReturn string
196+
* @return void
196197
*/
197-
protected function compile($compileKey, $shouldRespond, $shouldReturn)
198+
protected function compile($shouldRespond, $shouldReturn)
198199
{
199200
$mock = m::mock(
200201
'Estey\ReactMiddleware\CompileReact[getResponse]',
@@ -218,7 +219,7 @@ protected function compile($compileKey, $shouldRespond, $shouldReturn)
218219
$this->view
219220
->shouldReceive('with')
220221
->once()
221-
->with($compileKey, $shouldRespond)
222+
->with('content', $shouldRespond)
222223
->andReturn($this->view);
223224
}
224225

@@ -242,10 +243,10 @@ protected function compile($compileKey, $shouldRespond, $shouldReturn)
242243
*/
243244
public function testCompile()
244245
{
245-
$this->compile('foo', 'foo bar', 'foo bar baz');
246-
$this->compile('foo', null, 'foo bar baz');
247-
$this->compile('', (object) ['foo' => 'bar'], 'foo bar baz');
248-
$this->compile('', (object) ['foo' => 'bar', 'baz' => 'bar'], 'foo bar baz');
246+
$this->compile('foo bar', 'foo bar baz');
247+
$this->compile(null, 'foo bar baz');
248+
$this->compile((object) ['foo' => 'bar'], 'foo bar baz');
249+
$this->compile((object) ['foo' => 'bar', 'baz' => 'bar'], 'foo bar baz');
249250
}
250251

251252
/**
@@ -311,6 +312,8 @@ public function testGetCompilerUrlPrefixPath()
311312
/**
312313
* Helper method for testing the getResponse() method.
313314
*
315+
* @param $shouldRespond string
316+
* @param $shouldReturn string
314317
* @return void
315318
*/
316319
protected function getResponse($shouldRespond, $shouldReturn)

0 commit comments

Comments
 (0)