Shortcodes in your Laravel App
Created on November 29th, 2015
In this short video we'll show you how to inject code into your views using Middleware. You can create a shortcode in your view files and replace it with anything you would like using Middleware.
This will come in handy if you release a product and want to dynamically change content in your views from a shortcode. Here is the Middleware class that we used in the video:
<?php
namespace App\Http\Middleware;
use Closure;
class HomeShortcodeMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
if(!method_exists($response, 'content')):
return $response;
endif;
$content = str_replace('<!--[shortcode_hello]-->', '<!--[shortcode_hello]-->Hello There', $response->content());
$response->setContent($content);
return $response;
}
}
Hope you find this useful. Thanks :)
Comments (0)