A Laravel practise project. This project uses:
- PHP 7
- Laravel 5.6
- XAMPP
- Bootstrap 4.x
- Clear the cash of views:
php artisan view:clear
- Create a controller class:
php artisan make:controller PostController
- Create a model class:
php artisan make:model Flight
- Laravel 5.6 Documentations
- Pluralsight: Getting Started with Laravel (PHP Framework) - The Basics by Max Schwarzmueller (Completed)
- Pluralsight: Getting Started with Laravel (PHP Framework) - Models and Data by Max Schwarzmueller
- Pluralsight: RESTful Web Services with PHP and Laravel by Max Schwarzmueller
- Pluralsight: Play by Play: Getting Started with Laravel 5 with Steven Maguire
https://github.com/barryvdh/laravel-ide-helper
Dependency Injection (a good practise in general)
$request->input('mail')
Facades
Request::input("mail')
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Shorter syntax
{{ csrf_field() }}
Validation in Router
// routes/web.php
$validation = $validator->make($request->all(), [
'title' => 'required|min:5',
'content' => 'required|min:10'
]);
if ($validation->fails()) {
return redirect()->back()->withErrors($validation);
}
Validation in Controller
// PostController.php
$this->validate($request, [
'title' => 'required|min:5',
'content' => 'required|min:10'
]);
// automatically pass the error and redirect if there is an error
With no controller
Route::get('/', function () {
return view('blog.index');
})->name('blog.index');
With a controller
Route::get('/', 'PostController@getIndex')->name('blog.index');
Structured route syntax
Route::get('/', [
'uses' => 'PostController@getIndex',
'as => 'blog.index'
]);
Saving one-time data (Flashed Session Data) in route
https://laravel.com/docs/5.6/redirects#redirecting-with-flashed-session-data
return redirect()->route('admin.index')->with('info', 'Post edited, new Title: ' . $request->input('title'));
https://laravel.com/docs/5.6/blade
Using a master layout
File: layouts/admin.blade.php
<div class="container">
@yield('content')
</div>
File: admin/index.blade.php
@extends('layouts.admin')
@section('content')
something goes here
@endsection
if
@if(Session::has('info'))
something goes here
@else
something goes here
@endif
foreach
@foreach($posts as $post)
...
<a href="{{ route('admin.edit', ['id' => array_search($post, $posts)]) }}">Edit</a>
...
@endforeach
for
@for($i = 0; $i < 5; $i++)
<p>{{ $i + 1 }}.Iteration</p>
@endfor
Display data
<p>{{ "Hello" }}</p>
<p>{{ $name }}</p>
<p>{{ 2 == 2 ? "Equal" : "Does not equal" }}</p>
<p>{{ 2 == 3 ? "Equal" : "Does not equal" }}</p>
XSS Protected
{{ "<script>alert('Hello');</script>" }}
Raw data - Not protected
{!! "<script>alert('Hello');</script>" !!}}