SlideShare a Scribd company logo
Javascript Laravel's friend
Introduction
● Bart (@denbatte)
● Teacher
● PHP/Laravel enthousiast
● Slides will be available on slideshare
Objectives
● Creating the best game ever!
● Exploring Laravel by doing the previous.
● Learning how Javascript integrates with Laravel
– Not using JS Frameworks (Angular) for now!
Preparing Laravel with composer
composer.json
"require": {
"laravel/framework": "4.1.*",
"laracasts/utilities": "1.0",
// Development packages
"way/generators": "2.*",
"barryvdh/laravel-ide-helper": "1.*",
"fzaninotto/faker": "v1.3.0",
"fedeisas/laravel-js-routes": "1.3"
}
Do not forget to add the serviceProviders at app/config/app.php
The game - Login
The game - Dashboard
Road map
● Preparing Laravel
– Database and models
– Routes and controllers
● Adding Jquery and custom scripts using blade
● Giving PHP data to Javascript
– The easy way
– The even more easy “Jeffrey” way
● Making a ajax call to a controller
– Using named routes
– Named routes as import
Preparing the database
● Migration files for users/score table
● Seeding with faker
terminal
user@device: php artisan generate:migration create_<name>_table
user@device: php artisan generate:seed
user@device: php artisan migrate –-seed
Do not forget to add database settings at app/config/database.php
Preparing the database
● Migration files for users/score table
● Seeding with faker
terminal
user@device: php artisan migrate –-seed
Migration file Seed file
Generating database models
● A model will make Eloquent understand your data.
– User.php is already there
– Adding Score.php model
terminal
user@device: php artisan generate:model Score
Preparing routes
app/routes.php
// Game: sessions resource – login and logout
Route::resource('sessions', 'SessionsController');
Route::get('/', 'sessionsController@create');
Route::get('login', 'sessionsController@create');
Route::get('logout', 'sessionsController@destroy');
// Actual game view
Route::get('game', "ScoreController@highscore")->before("auth");
// Score: Ajax route retrieving high score
Route::post( '/score/{level}', array(
'as' => 'score.index',
'uses' => 'ScoreController@index'
) );
// Score: Ajax route updating player score
Route::post( '/score/update', array(
'as' => 'score.update',
'uses' => 'ScoreController@update'
) );
Generating controllers
terminal
user@device: php artisan generate:controller ScoreController
app/controllers/ScoreController.php
public function index($level = “normal”)
{
$scoreList = Score::where("level", "=", $level)
->take("5")
->join("users", "user.id", "=", "scores.user_id")
->orderBy(“score”, “desc”);
->get()
->toJson();
return $scoreList;
}
Road map
● Preparing Laravel
– Database and models
– Routes and controllers
● Adding Jquery and custom scripts using blade
● Giving PHP data to Javascript
– The easy way
– The even more easy “Jeffrey” way
● Making a ajax call to a controller
– Using named routes
– Named routes as import
Adding Jquery and custom scripts
● Using Laravel's blade templating engine
● Global scripts/css in master template
● Specific scripts/css in sub views
Code snippet
{{ HTML::script("js/jquery.js") }}
{{ HTML::style("css/style.css") }}
● Using a asset manager
● CodeSleeve/asset-pipeline gives tons of options
Adding Jquery and custom scripts
Using blade
● We are inserting into layouts/default.blade.php:
SCRIPTS
● JQuery + knob
● Game mechanics
● Demo purpose scripts
STYLES
● Fontawesome
● Game css
● Google Orbitron font
Have a look for yourself at layouts/default.blade.php!
Road map
● Preparing Laravel
– Database and models
– Routes and controllers
● Adding Jquery and custom scripts using blade
● Giving PHP data to Javascript
– The easy way
– The even more easy “Jeffrey” way
● Making a ajax call to a controller
– Using named routes
– Named routes as import
Giving PHP data to Javascript
Request
Response Grab response $
Giving PHP data to Javascript
The easy way
Giving PHP data to Javascript
PHP snippet
// Response with variable
$name = “denbatte”;
return View::make(“game”, compact(“name”));
HTML/JS snippet
// Grab response variable
<script> var name = “<?php echo $name; ?>”; </script>
// Laravel blade way
<script> var name = “{{ $name }}”;</script>
● Not very scalabe
● Javascript needs to be inline
Giving PHP data to Javascript
The even more easy
“Jeffrey” way
Giving PHP data to Javascript
PHP snippet
// Response with variable
$name = “denbatte”;
Javascript::put(array("name" => $name));
return View::make(“game”);
● Making use of the laracasts/utilities plugin
● Binds arrays with variables to one view!
● game.blade.php
Have a look for yourself at scoreController.php @ highscore
Giving PHP data to Javascript
● Setup:
● Composer
● Add serviceprovider
● Artisan config:publish
Have a look for yourself at config.php
terminal
user@device: php artisan config:publish laracasts/utilities
Road map
● Preparing Laravel
– Database and models
– Routes and controllers
● Adding Jquery and custom scripts using blade
● Giving PHP data to Javascript
– The easy way
– The even more easy “Jeffrey” way
● Making a ajax call to a controller
– Using named routes
– Named routes as import
Making a ajax call to a controller
Request
Response
Making a ajax call to a controller
Jquery code snippet
$.post("score/{level}").done(function(data) {
var data = $.parseJSON(data);
// Do stuff with the data
}, 'json');
scoreController@index
public function index($level = "normal")
{
$scoreList = Score::where("level", "=", $level)
->take("5")
->join("users", "users.id", "=", "scores.user_id")
->orderBy("score", "DESC")
->get()
->toJson();
return $scoreList;
}
routes.php
Have a look for yourself at scoreController.php @ index | lara...ajax.js
Using named routes
● Named routes? → laravel-js-routes package
– Generates a routes.js helper file in root.
– Gives a Router object
Layout.blade.php
{{ HTML::script("/path/to/routes.js") }}
Layout.blade.php
Router.route(route_name:string, params:array)
https://github.com/fedeisas/laravel-js-routes
Using named routes
● Import generated routes.js using JQuery or require.js
JQuery snippet
$.getScript( "ajax/test.js");
Questions
● Shoot, I will try to answer them now or I will
come back to you later.
● Now lets play this game!

More Related Content

What's hot (20)

PPTX
Laravel Beginners Tutorial 1
Vikas Chauhan
 
ODP
REST API Laravel
John Dave Decano
 
PPTX
Laravel for Web Artisans
Raf Kewl
 
PPTX
Laravel - Website Development in Php Framework.
SWAAM Tech
 
PDF
Laravel 5 In Depth
Kirk Bushell
 
PDF
Hello World on Slim Framework 3.x
Ryan Szrama
 
PPTX
API Development with Laravel
Michael Peacock
 
PPTX
Laravel Beginners Tutorial 2
Vikas Chauhan
 
PPTX
Laravel 5
Sudip Simkhada
 
PDF
Bullet: The Functional PHP Micro-Framework
Vance Lucas
 
PDF
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
PPTX
Laravel5 Introduction and essentials
Pramod Kadam
 
PPTX
Intro to Laravel
Azukisoft Pte Ltd
 
PPTX
Workshop Laravel 5.2
Wahyu Rismawan
 
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
 
PDF
Laravel 101
Commit University
 
KEY
Rails web api 开发
shaokun
 
PDF
Intro to Laravel 4
Singapore PHP User Group
 
KEY
More to RoC weibo
shaokun
 
PDF
Making the Most of Modern PHP in Drupal 7
Ryan Szrama
 
Laravel Beginners Tutorial 1
Vikas Chauhan
 
REST API Laravel
John Dave Decano
 
Laravel for Web Artisans
Raf Kewl
 
Laravel - Website Development in Php Framework.
SWAAM Tech
 
Laravel 5 In Depth
Kirk Bushell
 
Hello World on Slim Framework 3.x
Ryan Szrama
 
API Development with Laravel
Michael Peacock
 
Laravel Beginners Tutorial 2
Vikas Chauhan
 
Laravel 5
Sudip Simkhada
 
Bullet: The Functional PHP Micro-Framework
Vance Lucas
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
Laravel5 Introduction and essentials
Pramod Kadam
 
Intro to Laravel
Azukisoft Pte Ltd
 
Workshop Laravel 5.2
Wahyu Rismawan
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
 
Laravel 101
Commit University
 
Rails web api 开发
shaokun
 
Intro to Laravel 4
Singapore PHP User Group
 
More to RoC weibo
shaokun
 
Making the Most of Modern PHP in Drupal 7
Ryan Szrama
 

Similar to Javascript laravel's friend (20)

PDF
Getting Started-with-Laravel
Mindfire Solutions
 
PDF
Getting started-with-laravel
Nikhil Agrawal
 
PDF
How to Create and Load Model in Laravel
Yogesh singh
 
DOCX
Laravel
biplob04
 
PPTX
Introduction to laravel framework
Ahmad Fatoni
 
PPTX
What-is-Laravel-23-August-2017.pptx
AbhijeetKumar456867
 
PPTX
What-is-Laravel and introduciton to Laravel
PraveenHegde20
 
PPTX
CRUD presentation of laravel application.pptx
ShoukatRiaz
 
PDF
MidwestPHP 2016 - Adventures in Laravel 5
Joe Ferguson
 
PPTX
Laravel
Dyuti Islam
 
PPTX
introduction to Laravel and its Basic and origin
Karthik Rohan
 
PPTX
Lecture 4_Laravel Controller and Data Pass-Route.pptx
SaziaRahman
 
PPTX
Laravel ppt
Mayank Panchal
 
PPTX
Laravel overview
Obinna Akunne
 
ODP
Laravel 5.3 - Web Development Php framework
Swapnil Tripathi ( Looking for new challenges )
 
PPTX
Lecture 2_ Intro to laravel.pptx
SaziaRahman
 
PPTX
Getting started with laravel
Advance Idea Infotech
 
PPTX
Study-Guide 3 let Routing-in-Laravel.pptx
AquinoLaurence
 
PPTX
Laravel Crud Tutorial Basic Step by Stepy S
christopherneo4
 
PDF
laravel-interview-questions.pdf
AnuragMourya8
 
Getting Started-with-Laravel
Mindfire Solutions
 
Getting started-with-laravel
Nikhil Agrawal
 
How to Create and Load Model in Laravel
Yogesh singh
 
Laravel
biplob04
 
Introduction to laravel framework
Ahmad Fatoni
 
What-is-Laravel-23-August-2017.pptx
AbhijeetKumar456867
 
What-is-Laravel and introduciton to Laravel
PraveenHegde20
 
CRUD presentation of laravel application.pptx
ShoukatRiaz
 
MidwestPHP 2016 - Adventures in Laravel 5
Joe Ferguson
 
Laravel
Dyuti Islam
 
introduction to Laravel and its Basic and origin
Karthik Rohan
 
Lecture 4_Laravel Controller and Data Pass-Route.pptx
SaziaRahman
 
Laravel ppt
Mayank Panchal
 
Laravel overview
Obinna Akunne
 
Laravel 5.3 - Web Development Php framework
Swapnil Tripathi ( Looking for new challenges )
 
Lecture 2_ Intro to laravel.pptx
SaziaRahman
 
Getting started with laravel
Advance Idea Infotech
 
Study-Guide 3 let Routing-in-Laravel.pptx
AquinoLaurence
 
Laravel Crud Tutorial Basic Step by Stepy S
christopherneo4
 
laravel-interview-questions.pdf
AnuragMourya8
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Designing Production-Ready AI Agents
Kunal Rai
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Biography of Daniel Podor.pdf
Daniel Podor
 
July Patch Tuesday
Ivanti
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
Ad

Javascript laravel's friend

  • 2. Introduction ● Bart (@denbatte) ● Teacher ● PHP/Laravel enthousiast ● Slides will be available on slideshare
  • 3. Objectives ● Creating the best game ever! ● Exploring Laravel by doing the previous. ● Learning how Javascript integrates with Laravel – Not using JS Frameworks (Angular) for now!
  • 4. Preparing Laravel with composer composer.json "require": { "laravel/framework": "4.1.*", "laracasts/utilities": "1.0", // Development packages "way/generators": "2.*", "barryvdh/laravel-ide-helper": "1.*", "fzaninotto/faker": "v1.3.0", "fedeisas/laravel-js-routes": "1.3" } Do not forget to add the serviceProviders at app/config/app.php
  • 5. The game - Login
  • 6. The game - Dashboard
  • 7. Road map ● Preparing Laravel – Database and models – Routes and controllers ● Adding Jquery and custom scripts using blade ● Giving PHP data to Javascript – The easy way – The even more easy “Jeffrey” way ● Making a ajax call to a controller – Using named routes – Named routes as import
  • 8. Preparing the database ● Migration files for users/score table ● Seeding with faker terminal user@device: php artisan generate:migration create_<name>_table user@device: php artisan generate:seed user@device: php artisan migrate –-seed Do not forget to add database settings at app/config/database.php
  • 9. Preparing the database ● Migration files for users/score table ● Seeding with faker terminal user@device: php artisan migrate –-seed Migration file Seed file
  • 10. Generating database models ● A model will make Eloquent understand your data. – User.php is already there – Adding Score.php model terminal user@device: php artisan generate:model Score
  • 11. Preparing routes app/routes.php // Game: sessions resource – login and logout Route::resource('sessions', 'SessionsController'); Route::get('/', 'sessionsController@create'); Route::get('login', 'sessionsController@create'); Route::get('logout', 'sessionsController@destroy'); // Actual game view Route::get('game', "ScoreController@highscore")->before("auth"); // Score: Ajax route retrieving high score Route::post( '/score/{level}', array( 'as' => 'score.index', 'uses' => 'ScoreController@index' ) ); // Score: Ajax route updating player score Route::post( '/score/update', array( 'as' => 'score.update', 'uses' => 'ScoreController@update' ) );
  • 12. Generating controllers terminal user@device: php artisan generate:controller ScoreController app/controllers/ScoreController.php public function index($level = “normal”) { $scoreList = Score::where("level", "=", $level) ->take("5") ->join("users", "user.id", "=", "scores.user_id") ->orderBy(“score”, “desc”); ->get() ->toJson(); return $scoreList; }
  • 13. Road map ● Preparing Laravel – Database and models – Routes and controllers ● Adding Jquery and custom scripts using blade ● Giving PHP data to Javascript – The easy way – The even more easy “Jeffrey” way ● Making a ajax call to a controller – Using named routes – Named routes as import
  • 14. Adding Jquery and custom scripts ● Using Laravel's blade templating engine ● Global scripts/css in master template ● Specific scripts/css in sub views Code snippet {{ HTML::script("js/jquery.js") }} {{ HTML::style("css/style.css") }} ● Using a asset manager ● CodeSleeve/asset-pipeline gives tons of options
  • 15. Adding Jquery and custom scripts Using blade ● We are inserting into layouts/default.blade.php: SCRIPTS ● JQuery + knob ● Game mechanics ● Demo purpose scripts STYLES ● Fontawesome ● Game css ● Google Orbitron font Have a look for yourself at layouts/default.blade.php!
  • 16. Road map ● Preparing Laravel – Database and models – Routes and controllers ● Adding Jquery and custom scripts using blade ● Giving PHP data to Javascript – The easy way – The even more easy “Jeffrey” way ● Making a ajax call to a controller – Using named routes – Named routes as import
  • 17. Giving PHP data to Javascript Request Response Grab response $
  • 18. Giving PHP data to Javascript The easy way
  • 19. Giving PHP data to Javascript PHP snippet // Response with variable $name = “denbatte”; return View::make(“game”, compact(“name”)); HTML/JS snippet // Grab response variable <script> var name = “<?php echo $name; ?>”; </script> // Laravel blade way <script> var name = “{{ $name }}”;</script> ● Not very scalabe ● Javascript needs to be inline
  • 20. Giving PHP data to Javascript The even more easy “Jeffrey” way
  • 21. Giving PHP data to Javascript PHP snippet // Response with variable $name = “denbatte”; Javascript::put(array("name" => $name)); return View::make(“game”); ● Making use of the laracasts/utilities plugin ● Binds arrays with variables to one view! ● game.blade.php Have a look for yourself at scoreController.php @ highscore
  • 22. Giving PHP data to Javascript ● Setup: ● Composer ● Add serviceprovider ● Artisan config:publish Have a look for yourself at config.php terminal user@device: php artisan config:publish laracasts/utilities
  • 23. Road map ● Preparing Laravel – Database and models – Routes and controllers ● Adding Jquery and custom scripts using blade ● Giving PHP data to Javascript – The easy way – The even more easy “Jeffrey” way ● Making a ajax call to a controller – Using named routes – Named routes as import
  • 24. Making a ajax call to a controller Request Response
  • 25. Making a ajax call to a controller Jquery code snippet $.post("score/{level}").done(function(data) { var data = $.parseJSON(data); // Do stuff with the data }, 'json'); scoreController@index public function index($level = "normal") { $scoreList = Score::where("level", "=", $level) ->take("5") ->join("users", "users.id", "=", "scores.user_id") ->orderBy("score", "DESC") ->get() ->toJson(); return $scoreList; } routes.php Have a look for yourself at scoreController.php @ index | lara...ajax.js
  • 26. Using named routes ● Named routes? → laravel-js-routes package – Generates a routes.js helper file in root. – Gives a Router object Layout.blade.php {{ HTML::script("/path/to/routes.js") }} Layout.blade.php Router.route(route_name:string, params:array) https://github.com/fedeisas/laravel-js-routes
  • 27. Using named routes ● Import generated routes.js using JQuery or require.js JQuery snippet $.getScript( "ajax/test.js");
  • 28. Questions ● Shoot, I will try to answer them now or I will come back to you later. ● Now lets play this game!