Skip to content

Commit

Permalink
Merge pull request livewire#145 from faustbrian/feat/redirect-helpers
Browse files Browse the repository at this point in the history
add redirectRoute and redirectAction helpers
  • Loading branch information
calebporzio committed Aug 7, 2019
2 parents 0ecabb0 + b40f60d commit 4e538ae
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/Component.php
Expand Up @@ -13,12 +13,12 @@ abstract class Component
use Concerns\ValidatesInput,
Concerns\DetectsDirtyProperties,
Concerns\HandlesActions,
Concerns\PerformsRedirects,
Concerns\ReceivesEvents,
Concerns\InteractsWithProperties,
Concerns\TracksRenderedChildren;

public $id;
public $redirectTo;
protected $lifecycleHooks = [
'mount', 'updating', 'updated',
];
Expand Down Expand Up @@ -52,11 +52,6 @@ public function render()
return view("livewire.{$this->getName()}");
}

public function redirect($url)
{
$this->redirectTo = $url;
}

public function output($errors = null)
{
$view = $this->render();
Expand Down
23 changes: 23 additions & 0 deletions src/Concerns/PerformsRedirects.php
@@ -0,0 +1,23 @@
<?php

namespace Livewire\Concerns;

trait PerformsRedirects
{
public $redirectTo;

public function redirect($url)
{
$this->redirectTo = $url;
}

public function redirectRoute($name, $parameters = [], $absolute = true)
{
$this->redirectTo = route($name, $parameters, $absolute);
}

public function redirectAction($name, $parameters = [], $absolute = true)
{
$this->redirectTo = action($name, $parameters, $absolute);
}
}
39 changes: 39 additions & 0 deletions tests/RedirectTest.php
Expand Up @@ -19,6 +19,30 @@ public function standard_redirect()
$this->assertEquals('/local', $component->redirectTo);
}

/** @test */
public function route_redirect()
{
$this->registerNamedRoute();

$component = app(LivewireManager::class)->test(TriggersRedirectStub::class);

$component->runAction('triggerRedirectRoute');

$this->assertEquals('http://localhost/foo', $component->redirectTo);
}

/** @test */
public function action_redirect()
{
$this->registerAction();

$component = app(LivewireManager::class)->test(TriggersRedirectStub::class);

$component->runAction('triggerRedirectAction');

$this->assertEquals('http://localhost/foo', $component->redirectTo);
}

/** @test */
public function redirect_helper()
{
Expand Down Expand Up @@ -69,6 +93,11 @@ protected function registerNamedRoute()
return true;
})->name('foo');
}

protected function registerAction()
{
Route::get('foo', 'HomeController@index')->name('foo');
}
}

class TriggersRedirectStub extends Component
Expand All @@ -78,6 +107,16 @@ public function triggerRedirect()
return $this->redirect('/local');
}

public function triggerRedirectRoute()
{
return $this->redirectRoute('foo');
}

public function triggerRedirectAction()
{
return $this->redirectAction('HomeController@index');
}

public function triggerRedirectHelper()
{
return redirect('foo');
Expand Down

0 comments on commit 4e538ae

Please sign in to comment.