Digging into laravel is fun. Just made my first little generator tool. Laravel makes everything so bloody easy!
-
-
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 @stopAnd 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(‘partials.form’)... @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> ...
@yield(‘partials.form’)... @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.
-
Laravel global query scopes were the answer I needed when working out how to deal with post types in a clean way.
-
Great to see @laravelphp has reinstated auth/register functionality as an artisan command in 5.2
-
Updated to PHP7 and got white screen where was fine before. It was @laravelphp blade comment block causing. Deleting comment block fixed it. Just mentioned in case anybody else had a similar issue.
-
Starting a new personal project tonight. Finally a medium-sized project idea that I want to build. And one to really dig into Laravel.
-
Normally, with Laravel (@laravelphp on Twitter), if you create a model and use the “-m” or “–migration” flag to create the accompanying migration, it will pluralize the table.
So for example “User” model will use a “users” table; “Vehicle” model will use a “vehicles” table.
I just discovered however, that if you create a “Data” model, the table will also be created as “data” – not “datas”. It makes complete sense as datas isn’t a word.
I love using this framework.
-
Rewriting my database structure is proving more work than first thought. Still – I’ve learnt some new Laravel stuff which is always good. So even if it never ends up being used, it’s not been a wasted exercise.
-
On rethinking my database structure
I’ve been using Laravel for my personal website for about eight months now and haven’t looked back since.
This huge advantage to my rebuilding it in that way — from its original WordPress foundations — was that I had a crash course in using Laravel as well as learning some best practices as well.
Now that I have been using my site daily for all this time, I have found places where it has got messy — unavoidable through the learning experience — as well as places where I want to refine how I publish.
At the moment I am using what are called explicit post types. Meaning that I manually choose which post type a particular post belongs to before publishing — picking the appropriate create form to publish from.
However I have been thinking more and more about implicit post types — post types whose contents define what sort of post it is. Going the way of implicit may also see me abolishing the need for post-type-specific areas of my site.
I could still do this to some degree I suppose. e.g. If a post doesn’t have a title, it’s a note; If it has a location, it’s a checkin; etc.
The only way to see how this is going to go is to build it and actually publish with it for a while, I guess.
Update 8th February 2021: I now use WordPress for my website and have done for a few years now.
-
Bypassing Laravel’s CSRF Middleware on selected routes (from 5.1)
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.phpWithin 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.
-
With each extra Laravel component I pull in to use, I increasing think – why not just use Laravel? I then have to remind myself that this whole thing I’m building is an experiment and a learning exercise, so it’s okay.
-
Successfully pulled in Laravel’s Ioc Container package for use with my project. I love it when things just work. BooYa!