SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
What’s Laravel?
THE PHP FRAMEWORK FOR WEB ARTISANS.
PHP THAT DOESN'T HURT. CODE HAPPY & ENJOY THE FRESH AIR.
Where to start from?
Laravel is very flexible framework. There are at
least 3 options how to create new project:
- via laravel installer
- via composer
- clone from github
Via Laravel installer
This will download laravel installer via composer
- composer global require "laravel/installer=~1.1"
When installer added this simple command will create app
- laravel new <app name>
* Do not forget to add ~/.composer/vendor/bin to your PATH variable in ~/.bashrc
Other options
Via composer
- composer create-project laravel/laravel your-project-name
Get from GitHub
- https://github.com/laravel/laravel
- And then in the project dir run “composer install” to get all needed
packages
Development environment
Laravel keeps index.php file under public
directory meaning that when setting up virtual
host for app you need DocumentRoot to point
to public directory.
Any wild guesses why?
(please don’t use retarded http://localhost/mycoolproject/public)
Laravel and Composer
Using composer in Laravel you can
- Add/remove/update packages
- Dump autoload file and generate new one
- Update laravel version
Laravel directory structure
The app directory, as you might expect, contains the core code of your application.
The bootstrap folder contains a few files that bootstrap the framework and configure autoloading.
The app/config directory, as the name implies, contains all of your application's configuration files.
The app/database folder contains your database migration and seeds.
The public directory contains the front controller and your assets (images, JavaScript, CSS, etc.).
The app/storage directory contains compiled Blade templates, file based sessions, file caches, and
other files generated by the framework.
The tests directory contains your automated tests.
The vendor directory contains your Composer dependencies.
The app/model directory contains your model
The app/controllers directory contains your model
...
Magic Artisan
- Is located Laravel project root directory
- Basically is a php script which performs all actions in
Laravel for example:
- Manage migrations
- Check application routes
- Clear app cache
- Create Artisan commands(??)
- Run database seeds
Full list is available with “php artisan list”
Artisan commands
Artisan commands usually are some scripts launched from
command line or with cron. For example you need to have
daily export of your orders - write a command and run it
with cron.
- They accept options and
arguments
- Have pretty output if
needed
- Interactive (can ask
password/question)
Laravel Config
Laravel uses config files with arrays in it to
store different configurations. database.php ->
Location: app/config
To get config value simply follow dot notation
Config::get(‘<filename>.key1.key2.key3’);
Can also pass default value on not found
Config::get(‘key’, 123);
Laravel environments
Are stored in app/bootstrap/start.php How it can look:
$env = $app->detectEnvironment(array(
‘local’ => array(‘<my local hostname>’, ‘<someones other>’),
‘live’ => array(‘<live hostname>’),
‘arturs_local’ => array(‘<arturs local hostname>’),
));
Are used for configuration files, meaning that each
environment can have it’s own config.
So basically it means that under app/config
directory you will have more directories with
environment names.
In overrided files you should put only those
variables which need to be overridden.
Laravel environments
Laravel actively uses php namespaces to keep classnames
short and keep possibility to use same class names for
different components.
I would suggest everyone to use namespaces too. For
example all Admin functionality under Admin namespace.
Laravel and namespaces
So Laravel is MVC framework meaning
we have folder for controllers, views and
models by default, no need to create
them.
Guess there’s no need to explain MVC
pattern.
Laravel MVC
Defining Laravel routes is dead simple and there are lots of
ways to do it. All routes are defined in app/routes.php
- Simplest get route:
Route::get('/', function() {
return 'Hello World';
});
- More advanced named route (still get method):
Route::get('user/profile', array('as' => 'profile', function() {
//
}));
Laravel Routes
In the previous example you had to define every route by
hand. Isn’t very convenient though. So you can define
controller prefixes and let Laravel decide which action to
use. So in routes.php
Route::controller('user', 'UserController');
Route::controller('product', 'ProductController');
And in UserController you will have methods like “getLogout”, “postLogin”,
“getLogin” where post|get is type of request and logout is the second part of
request url : http://example.com/user/login
Laravel Routes
- Filters are run before or after some controller
action
- Are defined in app/filters.php
- There are global App::before and App::after
filters.
- Filters can be binded to multiple controllers
Laravel Filters
There’s one main Controller class
which all controllers should
extend. By default Laravel has
BaseController (extends from
Controller) and HomeController
(extends from BaseController)
Basic example from default
Laravel installation:
Laravel controllers
More advanced
example.
Laravel controllers
Responds only to get
method and returns
rendered view
Responds to post
method, and returns
redirect to next logical
action
Talking about Views
- All views are located in app/views directory
- Can be separated in subdirectories
- Can be both blade or simple php files
It is recommended to use balde template engine since it is
very convenient and helps to eliminate random logic blocks
in views
Insights in blade
Echo data simple way:
Hello, {{{ $name }}}.
The current UNIX timestamp is {{{ time() }}}.
Hello, {{{ $name or ‘John Doe’ }}} gets rendered as <?php echo isset($name) ? $name : ‘John Doe’ ?>
Don’t escape data with htmlentities:
Hello, {{ $name }}.
Insights in blade
Comments:
{{-- Comment visible only in blade file --}}
Loops:
@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
Conditions:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
@unless (Auth::check())
You are not signed in.
@endunless
Return view from controller
Views are also accessed by dot notation from
view directory.
So if we have app/views/user/profile.blade.php then to
make this view View::make(‘user.profile’, $data) Where
data is key value array with data used in template.
Models
- Models are located
under app/models
directory.
- Simple Product
model.
- Will use ‘products’
table unless another
defined
Laravel ORM
Why is it good?
- Has a lot of useful methods
- Is very flexible
- Has built in safe delete functionality
- Has built in Relationship functionality
- Has option to define scopes
Laravel ORM
- Models can have relations defined in
them for easier access to properties.
- $product->category in this case will
return Category model object where
this product belongs. How?
Laravel assumes you have category_id
in your products table, so when you call
$product->category query SELECT *
FROM ‘categories’ where id = ‘?’ is
performed. Of course you can define
different relation fields
Laravel ORM
Defining scope:
class User extends Eloquent {
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeWomen($query)
{
return $query->whereGender('W');
}
}
Using scope:
$users = User::popular()
->women()
->orderBy('created_at')
->get();
Laravel ORM
Cool methods:
Basically Laravels ORM has function for anything
// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));
// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(array('name' => 'John'));
Laravel migrations
- Interaction with migrations is happening
through artisan commands.
- Each migration has two functions up and down
to migrate and rollback
Laravel migrations
This is how basic migration looks like
DB seeds
Seeds are used to insert predefined data in
tables so there is something to start from for
example on development environment we can
create test users, test products and so on.
Recap
- Laravel is fast
- Flexible
- Easy to learn
- Has a great documentation
- really simple to install
- Very popular
???

Weitere ähnliche Inhalte

Andere mochten auch

Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksPhill Sparks
 
HTML5 Presentation
HTML5 PresentationHTML5 Presentation
HTML5 PresentationTony Bowens
 
Laravel workshops 1
Laravel workshops 1Laravel workshops 1
Laravel workshops 1Kamil Fojuth
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLin Yo-An
 
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)daylerees
 
Laravel - Veszprémi Technology Meetup
Laravel - Veszprémi Technology MeetupLaravel - Veszprémi Technology Meetup
Laravel - Veszprémi Technology MeetupBálint Szekeres
 
Laravel introduction
Laravel introductionLaravel introduction
Laravel introductionSimon Funk
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5Darren Craig
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5Bukhori Aqid
 
PHP Composer : Pourquoi ? Comment ? Et plus ...
PHP Composer : Pourquoi ? Comment ? Et plus ...PHP Composer : Pourquoi ? Comment ? Et plus ...
PHP Composer : Pourquoi ? Comment ? Et plus ...Romain Cambien
 
Laravel 5: Entenda o ambiente e a estrutura MVC
 Laravel 5: Entenda o ambiente e a estrutura MVC Laravel 5: Entenda o ambiente e a estrutura MVC
Laravel 5: Entenda o ambiente e a estrutura MVCMichael Douglas
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Viral Solani
 
A introduction to Laravel framework
A introduction to Laravel frameworkA introduction to Laravel framework
A introduction to Laravel frameworkPhu Luong Trong
 
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3harisonmtd
 

Andere mochten auch (20)

Why Laravel?
Why Laravel?Why Laravel?
Why Laravel?
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill Sparks
 
HTML5 Presentation
HTML5 PresentationHTML5 Presentation
HTML5 Presentation
 
Laravel workshops 1
Laravel workshops 1Laravel workshops 1
Laravel workshops 1
 
Hello Laravel 5!
Hello Laravel 5!Hello Laravel 5!
Hello Laravel 5!
 
Laravel Meetup
Laravel MeetupLaravel Meetup
Laravel Meetup
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHP
 
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
 
Laravel - Veszprémi Technology Meetup
Laravel - Veszprémi Technology MeetupLaravel - Veszprémi Technology Meetup
Laravel - Veszprémi Technology Meetup
 
Laravel introduction
Laravel introductionLaravel introduction
Laravel introduction
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5
 
PHP Composer : Pourquoi ? Comment ? Et plus ...
PHP Composer : Pourquoi ? Comment ? Et plus ...PHP Composer : Pourquoi ? Comment ? Et plus ...
PHP Composer : Pourquoi ? Comment ? Et plus ...
 
Laravel 101
Laravel 101Laravel 101
Laravel 101
 
Laravel 5: Entenda o ambiente e a estrutura MVC
 Laravel 5: Entenda o ambiente e a estrutura MVC Laravel 5: Entenda o ambiente e a estrutura MVC
Laravel 5: Entenda o ambiente e a estrutura MVC
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
 
A introduction to Laravel framework
A introduction to Laravel frameworkA introduction to Laravel framework
A introduction to Laravel framework
 
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
 

Kürzlich hochgeladen

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Laravel presentation by Arturs Lataks

  • 1.
  • 2. What’s Laravel? THE PHP FRAMEWORK FOR WEB ARTISANS. PHP THAT DOESN'T HURT. CODE HAPPY & ENJOY THE FRESH AIR.
  • 3. Where to start from? Laravel is very flexible framework. There are at least 3 options how to create new project: - via laravel installer - via composer - clone from github
  • 4. Via Laravel installer This will download laravel installer via composer - composer global require "laravel/installer=~1.1" When installer added this simple command will create app - laravel new <app name> * Do not forget to add ~/.composer/vendor/bin to your PATH variable in ~/.bashrc
  • 5. Other options Via composer - composer create-project laravel/laravel your-project-name Get from GitHub - https://github.com/laravel/laravel - And then in the project dir run “composer install” to get all needed packages
  • 6. Development environment Laravel keeps index.php file under public directory meaning that when setting up virtual host for app you need DocumentRoot to point to public directory. Any wild guesses why? (please don’t use retarded http://localhost/mycoolproject/public)
  • 7. Laravel and Composer Using composer in Laravel you can - Add/remove/update packages - Dump autoload file and generate new one - Update laravel version
  • 8. Laravel directory structure The app directory, as you might expect, contains the core code of your application. The bootstrap folder contains a few files that bootstrap the framework and configure autoloading. The app/config directory, as the name implies, contains all of your application's configuration files. The app/database folder contains your database migration and seeds. The public directory contains the front controller and your assets (images, JavaScript, CSS, etc.). The app/storage directory contains compiled Blade templates, file based sessions, file caches, and other files generated by the framework. The tests directory contains your automated tests. The vendor directory contains your Composer dependencies. The app/model directory contains your model The app/controllers directory contains your model ...
  • 9. Magic Artisan - Is located Laravel project root directory - Basically is a php script which performs all actions in Laravel for example: - Manage migrations - Check application routes - Clear app cache - Create Artisan commands(??) - Run database seeds Full list is available with “php artisan list”
  • 10. Artisan commands Artisan commands usually are some scripts launched from command line or with cron. For example you need to have daily export of your orders - write a command and run it with cron. - They accept options and arguments - Have pretty output if needed - Interactive (can ask password/question)
  • 11. Laravel Config Laravel uses config files with arrays in it to store different configurations. database.php -> Location: app/config To get config value simply follow dot notation Config::get(‘<filename>.key1.key2.key3’); Can also pass default value on not found Config::get(‘key’, 123);
  • 12. Laravel environments Are stored in app/bootstrap/start.php How it can look: $env = $app->detectEnvironment(array( ‘local’ => array(‘<my local hostname>’, ‘<someones other>’), ‘live’ => array(‘<live hostname>’), ‘arturs_local’ => array(‘<arturs local hostname>’), )); Are used for configuration files, meaning that each environment can have it’s own config.
  • 13. So basically it means that under app/config directory you will have more directories with environment names. In overrided files you should put only those variables which need to be overridden. Laravel environments
  • 14. Laravel actively uses php namespaces to keep classnames short and keep possibility to use same class names for different components. I would suggest everyone to use namespaces too. For example all Admin functionality under Admin namespace. Laravel and namespaces
  • 15. So Laravel is MVC framework meaning we have folder for controllers, views and models by default, no need to create them. Guess there’s no need to explain MVC pattern. Laravel MVC
  • 16. Defining Laravel routes is dead simple and there are lots of ways to do it. All routes are defined in app/routes.php - Simplest get route: Route::get('/', function() { return 'Hello World'; }); - More advanced named route (still get method): Route::get('user/profile', array('as' => 'profile', function() { // })); Laravel Routes
  • 17. In the previous example you had to define every route by hand. Isn’t very convenient though. So you can define controller prefixes and let Laravel decide which action to use. So in routes.php Route::controller('user', 'UserController'); Route::controller('product', 'ProductController'); And in UserController you will have methods like “getLogout”, “postLogin”, “getLogin” where post|get is type of request and logout is the second part of request url : http://example.com/user/login Laravel Routes
  • 18. - Filters are run before or after some controller action - Are defined in app/filters.php - There are global App::before and App::after filters. - Filters can be binded to multiple controllers Laravel Filters
  • 19. There’s one main Controller class which all controllers should extend. By default Laravel has BaseController (extends from Controller) and HomeController (extends from BaseController) Basic example from default Laravel installation: Laravel controllers
  • 20. More advanced example. Laravel controllers Responds only to get method and returns rendered view Responds to post method, and returns redirect to next logical action
  • 21. Talking about Views - All views are located in app/views directory - Can be separated in subdirectories - Can be both blade or simple php files It is recommended to use balde template engine since it is very convenient and helps to eliminate random logic blocks in views
  • 22. Insights in blade Echo data simple way: Hello, {{{ $name }}}. The current UNIX timestamp is {{{ time() }}}. Hello, {{{ $name or ‘John Doe’ }}} gets rendered as <?php echo isset($name) ? $name : ‘John Doe’ ?> Don’t escape data with htmlentities: Hello, {{ $name }}.
  • 23. Insights in blade Comments: {{-- Comment visible only in blade file --}} Loops: @forelse($users as $user) <li>{{ $user->name }}</li> @empty <p>No users</p> @endforelse Conditions: @if (count($records) === 1) I have one record! @elseif (count($records) > 1) I have multiple records! @else I don't have any records! @endif @unless (Auth::check()) You are not signed in. @endunless
  • 24. Return view from controller Views are also accessed by dot notation from view directory. So if we have app/views/user/profile.blade.php then to make this view View::make(‘user.profile’, $data) Where data is key value array with data used in template.
  • 25. Models - Models are located under app/models directory. - Simple Product model. - Will use ‘products’ table unless another defined
  • 26. Laravel ORM Why is it good? - Has a lot of useful methods - Is very flexible - Has built in safe delete functionality - Has built in Relationship functionality - Has option to define scopes
  • 27. Laravel ORM - Models can have relations defined in them for easier access to properties. - $product->category in this case will return Category model object where this product belongs. How? Laravel assumes you have category_id in your products table, so when you call $product->category query SELECT * FROM ‘categories’ where id = ‘?’ is performed. Of course you can define different relation fields
  • 28. Laravel ORM Defining scope: class User extends Eloquent { public function scopePopular($query) { return $query->where('votes', '>', 100); } public function scopeWomen($query) { return $query->whereGender('W'); } } Using scope: $users = User::popular() ->women() ->orderBy('created_at') ->get();
  • 29. Laravel ORM Cool methods: Basically Laravels ORM has function for anything // Retrieve the user by the attributes, or create it if it doesn't exist... $user = User::firstOrCreate(array('name' => 'John')); // Retrieve the user by the attributes, or instantiate a new instance... $user = User::firstOrNew(array('name' => 'John'));
  • 30. Laravel migrations - Interaction with migrations is happening through artisan commands. - Each migration has two functions up and down to migrate and rollback
  • 31. Laravel migrations This is how basic migration looks like
  • 32. DB seeds Seeds are used to insert predefined data in tables so there is something to start from for example on development environment we can create test users, test products and so on.
  • 33. Recap - Laravel is fast - Flexible - Easy to learn - Has a great documentation - really simple to install - Very popular
  • 34. ???