Finally got my site to use the roots wordpress structure. Composer dependency management and deployment with laravel forge. đ¤
Tag: PHP
How to easily set a custom redirect in Laravel form requests
In Laravel you can create custom request classes where you can house the validation for any given route. If that validation then fails, Laravelâs default action is to redirect the visitor back to the previous page. This is commonly used for when a form is submitted incorrectly – The visitor will be redirected back to said form to correct the errors. Sometimes, however, you may wish to redirect the visitor to a different location altogether.
TL;DR (Too long; didn’t read)
At the top of your custom request class, add one of the following protected properties and give it your required value. I have given example values to demonstrate:
protected $redirect = '/custom-page'; // Any URL or path
protected $redirectRoute = 'pages.custom-page'; // The named route of the page
protected $redirectAction = 'PagesController@customPage'; // The controller action to use.
This will then redirect your visitor to that location should they fail any of the validation checks within your custom form request class.
Explaination
When you create a request class through the Laravel artisan command, it will create one that extends the base Laravel class Illuminate\Foundation\Http\FormRequest
. Within this class the three protected properties listed above are initialised from line 33, but not set to a value.
Then further down the page of the base class, on line 127 at the time of writing, there is a protected method called getRedirectUrl
. This method performs a series of checks for whether or not any of the three redirect properties have actually been set. The first one it finds to be set by you, in the order given above, is the one that will be used as the custom redirect location.
Here is that getRedirectUrl
method for your convenience:
/**
* Get the URL to redirect to on a validation error.
*
* @return string
*/
protected function getRedirectUrl()
{
$url = $this->redirector->getUrlGenerator();
if ($this->redirect) {
return $url->to($this->redirect);
} elseif ($this->redirectRoute) {
return $url->route($this->redirectRoute);
} elseif ($this->redirectAction) {
return $url->action($this->redirectAction);
}
return $url->previous();
}
Do you have any extra tips to add to this? Let me know in the comments below.
Thanks.
Laravel Blade push and stack
Laravelâs blade view compiler is second to none. Iâve used a couple of different templating engines and blade is by far my favourite.
Including Partials
The way in which we include partials of views within our main views is as follows:@include(âpartials.my-first-partialâ)
It will inject that partialâs content in the specified place.
Defining Sections
Within our views, we define âsectionsâ with the following syntax:
@section(âsection_nameâ) The sectionâs content within here @stop
And we can define as many sections as we need for our project.
When the same section is used in multiple places within one compilation
Imagine we have master template file as such:
// layouts.main.blade.php <!doctype html> ...
... @yield(âcustom_scriptsâ)
Letâs suppose we have the following layout template that extends our main layout one and is including three partials. This example is a form template including its various inputs from separate partials. For my own website I have a different form for each of my post types and so I have the inputs in separate partials for easy reuse.
// partials.form.blade.php @extends(âlayouts.mainâ) <form>@include(âparials.form-titleâ) @include(âparials.form-contentâ) @include(âparials.form-tagsâ)</form>
Letâs next suppose that in a couple of those partial input views you need to inject some custom scripting. This is a slightly contrived example, but it will illustrate the point.
// partials.form-content.blade.php <textarea class="content" name="content"></textarea> @section(âcustom_scriptsâ) // dummy javascript as example $(â.contentâ).doSomething(); @stop
// partials.form-tags.blade.php <select class="tags" name="tags"> <option value="tagone">Tag One</option> <option value="tagtwo">Tag Two</option> <option value="tagthree">Tag Three</option> </select> @section(âcustom_scriptsâ) $(â.tagsâ).doSomethingElse() @stop
Now, when the form page gets compiled, only the first occurrence of the âcustom_scriptsâ section will be included.
So what if you needed to be able to define this section in chunks across partials?
Introducing Bladeâs Push & Stack directives
To give this functionality, Laravel does in fact have two little-known directives called âpushâ and âstackâ.
They allow you to âstack upâ items across partials with the âpushâ directive, which can then be echoed out together with the âstackâ directive.
Hereâs the above form example but with âpushâ and âstackâ used in place of âsectionâ and âyieldâ.
// layouts.main.blade.php <!doctype html> ...
... @stack(âcustom_scriptsâ)
// partials.form-content.blade.php <textarea class="content" name="content"></textarea> @push(âcustom_scriptsâ) // dummy javascript as example $(â.contentâ).doSomething(); @endpush
// partials.form-tags.blade.php <select class="tags" name="tags"> <option value="tagone">Tag One</option> <option value="tagtwo">Tag Two</option> <option value="tagthree">Tag Three</option> </select> @push(âcustom_scriptsâ) $(â.tagsâ).doSomethingElse() @endpush
This will now compile all uses of the @push(âcustom_scriptsâ) and echo them out as one wherever you call @stack(âcustom_scriptsâ)
When I was shown this technique by a mate at work, it blew my mind.
Have fun.
Bypassing Laravel’s CSRF Middleware on selected routes (from 5.1)
A handy way to have some of your routes skip the middleware for CSRF protection. Handy in some situations.
Laravel does a great job at protecting us from cross-site request forgeries – or C.S.R.F. for short.But sometimes you may not wish to have that layer present. Well with Laravel 5.1 you can very easily bypass this middleware, simply by populating an array in the following file:
app/Http/Middleware/VerifyCsrfToken.php
Within this class you can add a protected propertyâââan arrayâââcalled $except
, which will tell Laravel to use this middleware except for the ones you specify here.
A complete example could be:
protected $except = [
'ignore/this/url',
'this/one/too',
'and/this',
];
So for those three URLs, the CSRF middleware would be skipped.