PHP Cheatsheet
Server-side scripting for the web
Getting Started
Install via Composer
Create a new Laravel project and start the development server.
composer create-project laravel/laravel example-app
cd example-app
php artisan serveSail Setup (Linux/macOS)
Install Laravel with Docker using Sail.
curl -s https://laravel.build/example-app | bash
cd example-app
./vendor/bin/sail upAccess App
Default local development URL.
http://localhostEnvironment & Config
Access .env Values
Get environment variables with optional default.
env('APP_DEBUG');
env('APP_DEBUG', false);App Environment
Get current app environment.
use Illuminate\Support\Facades\App;
$env = App::environment();Config Access
Access config values with optional fallback.
config('app.timezone');
config('app.timezone', 'UTC');Routing
Basic Route
Simple GET route using closure.
Route::get('/hello', function () {
return 'Hello World';
});Controller Route
Define route with controller method.
Route::get('/user', [UserController::class, 'index']);Named Route
Create a named route for reuse.
Route::get('/profile', function () {
// ...
})->name('profile');Route Parameters
Required Param
Capture route parameters from URI.
Route::get('/user/{id}', function ($id) {
return $id;
});Optional Param
Make a route parameter optional.
Route::get('/user/{name?}', function ($name = 'Guest') {
return $name;
});Middleware & Groups
Middleware Group
Apply middleware to multiple routes.
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
return 'Dashboard';
});
});Route Prefix
Group routes under a common URL prefix.
Route::prefix('admin')->group(function () {
Route::get('/users', function () {
return 'Admin Users';
});
});Blade Templates
Echo Output
Display data in Blade templates.
<h1>Hello, {{ $name }}</h1>If Statement
Conditional logic in Blade.
@if($count > 0)
Count: {{ $count }}
@endifLoop Example
Iterate over arrays or collections.
@foreach ($users as $user)
{{ $user->name }}
@endforeachControllers
Basic Controller
Basic Laravel controller example.
class UserController extends Controller {
public function show($id) {
return view('user.profile', ['user' => User::find($id)]);
}
}Route to Controller
Route mapped to controller method.
Route::get('/user/{id}', [UserController::class, 'show']);Requests
CSRF Token
Protect forms from CSRF attacks.
<form method="POST" action="/profile">
@csrf
...
</form>Access Request Data
Read input from form submissions.
$name = $request->input('name');Old Input
Repopulate form inputs after validation error.
<input value="{{ old('email') }}" />Validation
Validate Request
Validate incoming request data.
$request->validate([
'title' => 'required|max:255',
'body' => 'required',
]);Validation Errors
Display validation errors in Blade.
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endifCommands
Run Dev Server
Start Laravel dev server at http://localhost:8000
php artisan serveClear Cache
Clear all Laravel caches.
php artisan config:clear
php artisan route:clear
php artisan view:clearMigrate DB
Run the database migrations.
php artisan migrate