I was working on an application and I needed to output a dynamic javascript file for inclusion from external websites with the [script src=””] tag. Also I needed to pass some variables to the view which is using the Blade engine.

For a quick solution to serving up a Javascript or CSS file using Laravel routes, i.e. http://www.example.com/js-file, take a look at the following snippet.

public function serve_js()
{
$contents = View::make('javascript.client-js', [ 'data' => $data //If you are passing extra data ]);
$response = Response::make($contents, 200);
$response->header('Content-Type', 'application/javascript');
return $response;
}

A file exists in app/views/javascript/ folder named client-js.blade.php. This file only contains the javascript needed to be served. E.g

$(function(){
$('#geoamplify-popup').dialogBox({
width: 500,
height: 300,
autoSize: true,
autoHide: false,
time: 3000,
effect: 'fall',
type: 'correct',
title: '',
hasClose: true,
content: '{{ $content }}' });
})

This code can be adapted to serve CSS as well, just change Content-Type to be text/css.