Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit 0cabb27

Browse files
authored
Merge pull request #93 from PostHog/laravel-skill
Laravel skill
2 parents 3a9cd92 + 02ec646 commit 0cabb27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+8260
-4
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ __pycache__/
3535
*.pyo
3636
db.sqlite3
3737

38+
# PHP / Laravel
39+
vendor/
40+
composer.phar
41+
storage/framework/cache/*
42+
storage/framework/sessions/*
43+
storage/framework/views/*
44+
storage/logs/*
45+
bootstrap/cache/*
46+
*.sqlite
47+
*.sqlite-journal
48+
3849
# Environment variables
3950
.env
4051
.env.local

basics/laravel/.env.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
APP_NAME="PostHog Laravel Example"
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost:8000
6+
7+
DB_CONNECTION=sqlite
8+
# DB_DATABASE will use default database/database.sqlite
9+
10+
CACHE_DRIVER=file
11+
CACHE_STORE=file
12+
13+
SESSION_DRIVER=file
14+
SESSION_LIFETIME=120
15+
16+
POSTHOG_API_KEY=your_posthog_api_key_here
17+
POSTHOG_HOST=https://us.i.posthog.com
18+
POSTHOG_DISABLED=false

basics/laravel/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Keep directory structure with .gitkeep files
2+
!/storage/framework/cache/.gitkeep
3+
!/storage/framework/sessions/.gitkeep
4+
!/storage/framework/views/.gitkeep
5+
!/storage/logs/.gitkeep
6+
!/bootstrap/cache/.gitkeep
7+
8+
# Laravel-specific
9+
/public/hot
10+
/public/storage
11+
.env.backup

basics/laravel/IMPLEMENTATION.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# Laravel PostHog Example - Implementation Summary
2+
3+
This document summarizes the implementation of the Laravel PostHog example application, ported from the Flask version.
4+
5+
## ✅ Completed Implementation
6+
7+
### Core Application Structure
8+
9+
**Models & Database**
10+
- ✅ User model with PostHog properties helper method
11+
- ✅ User migration with `is_staff` field
12+
- ✅ Database seeder for default admin user
13+
- ✅ SQLite database configuration
14+
15+
**PostHog Integration**
16+
- ✅ PostHog configuration file (`config/posthog.php`)
17+
- ✅ PostHogService class with all core methods:
18+
- `identify()` - User identification
19+
- `capture()` - Event tracking
20+
- `captureException()` - Error tracking
21+
- `isFeatureEnabled()` - Feature flag checking
22+
- `getFeatureFlagPayload()` - Feature flag payload retrieval
23+
24+
**Authentication (Livewire Components)**
25+
- ✅ Login component with PostHog tracking
26+
- ✅ Register component with PostHog tracking
27+
- ✅ Logout route with PostHog tracking
28+
29+
**Core Features (Livewire Components)**
30+
- ✅ Dashboard - Feature flag demonstration
31+
- ✅ Burrito Tracker - Custom event tracking
32+
- ✅ Profile - Error tracking demonstration
33+
34+
**API Controllers**
35+
- ✅ BurritoController - API endpoint for burrito tracking
36+
- ✅ ErrorTestController - Manual error capture demonstration
37+
38+
**Views & Layouts**
39+
- ✅ App layout (authenticated users)
40+
- ✅ Guest layout (unauthenticated users)
41+
- ✅ All Livewire view files with inline styling
42+
- ✅ Error pages (404, 500)
43+
44+
**Routes**
45+
- ✅ Web routes (authentication, dashboard, burrito, profile, logout)
46+
- ✅ API routes (burrito tracking, error testing)
47+
48+
**Configuration**
49+
- ✅ Environment example file
50+
- ✅ Composer.json with dependencies
51+
- ✅ Laravel config files (app, auth, database, session)
52+
- ✅ .gitignore
53+
54+
**Documentation**
55+
- ✅ Comprehensive README
56+
- ✅ Implementation plan (php-plan.md)
57+
58+
## 📋 Features Implemented
59+
60+
### 1. User Authentication
61+
- Login with PostHog identification
62+
- Registration with PostHog tracking
63+
- Logout with event capture
64+
- Session management
65+
66+
### 2. PostHog Analytics
67+
- User identification on login/signup
68+
- Person properties (email, is_staff, date_joined)
69+
- Custom event tracking (burrito considerations)
70+
- Dashboard views tracking
71+
72+
### 3. Feature Flags
73+
- Feature flag checking (`new-dashboard-feature`)
74+
- Feature flag payload retrieval
75+
- Conditional UI rendering based on flags
76+
77+
### 4. Error Tracking
78+
- Manual exception capture
79+
- Error ID generation
80+
- Test endpoint with optional capture (`?capture=true/false`)
81+
82+
### 5. UI/UX
83+
- Responsive layouts
84+
- Flash messages for user feedback
85+
- Livewire reactivity for burrito counter
86+
- Loading states on buttons
87+
88+
## 🎯 PostHog Integration Points
89+
90+
| Feature | Location | PostHog Method |
91+
|---------|----------|----------------|
92+
| User Login | `Login.php:23-27` | `identify()` + `capture()` |
93+
| User Signup | `Register.php:29-32` | `identify()` + `capture()` |
94+
| User Logout | `web.php:25` | `capture()` |
95+
| Dashboard View | `Dashboard.php:18` | `capture()` |
96+
| Feature Flag Check | `Dashboard.php:21-25` | `isFeatureEnabled()` |
97+
| Feature Flag Payload | `Dashboard.php:28-31` | `getFeatureFlagPayload()` |
98+
| Burrito Tracking | `BurritoTracker.php:22-24` | `identify()` + `capture()` |
99+
| Profile View | `Profile.php:14` | `capture()` |
100+
| Error Capture | `ErrorTestController.php:22-24` | `identify()` + `captureException()` |
101+
102+
## 📁 File Structure
103+
104+
```
105+
basics/laravel/
106+
├── app/
107+
│ ├── Http/
108+
│ │ ├── Controllers/
109+
│ │ │ ├── Controller.php
110+
│ │ │ └── Api/
111+
│ │ │ ├── BurritoController.php
112+
│ │ │ └── ErrorTestController.php
113+
│ │ └── Livewire/
114+
│ │ ├── Auth/
115+
│ │ │ ├── Login.php
116+
│ │ │ └── Register.php
117+
│ │ ├── Dashboard.php
118+
│ │ ├── BurritoTracker.php
119+
│ │ └── Profile.php
120+
│ ├── Models/
121+
│ │ └── User.php
122+
│ └── Services/
123+
│ └── PostHogService.php
124+
├── config/
125+
│ ├── app.php
126+
│ ├── auth.php
127+
│ ├── database.php
128+
│ ├── posthog.php
129+
│ └── session.php
130+
├── database/
131+
│ ├── migrations/
132+
│ │ └── 2024_01_01_000000_create_users_table.php
133+
│ └── seeders/
134+
│ └── DatabaseSeeder.php
135+
├── resources/
136+
│ └── views/
137+
│ ├── components/
138+
│ │ └── layouts/
139+
│ │ ├── app.blade.php
140+
│ │ └── guest.blade.php
141+
│ ├── livewire/
142+
│ │ ├── auth/
143+
│ │ │ ├── login.blade.php
144+
│ │ │ └── register.blade.php
145+
│ │ ├── dashboard.blade.php
146+
│ │ ├── burrito-tracker.blade.php
147+
│ │ └── profile.blade.php
148+
│ └── errors/
149+
│ ├── 404.blade.php
150+
│ └── 500.blade.php
151+
├── routes/
152+
│ ├── api.php
153+
│ └── web.php
154+
├── .env.example
155+
├── .gitignore
156+
├── composer.json
157+
├── IMPLEMENTATION.md
158+
└── README.md
159+
```
160+
161+
## 🔄 Flask to Laravel Mapping
162+
163+
| Flask Component | Laravel Equivalent |
164+
|----------------|-------------------|
165+
| Flask-Login | Laravel Auth + Livewire |
166+
| Flask-SQLAlchemy | Eloquent ORM |
167+
| Jinja2 Templates | Blade Templates + Livewire |
168+
| Blueprint routes | Route definitions |
169+
| @app.route decorators | Route::get/post |
170+
| session | session() helper |
171+
| flash() | session()->flash() |
172+
| @login_required | Route::middleware('auth') |
173+
| request.form | Livewire properties |
174+
| render_template() | view() or Livewire render() |
175+
| jsonify() | response()->json() |
176+
| SQLAlchemy models | Eloquent models |
177+
178+
## 🚀 Next Steps for Production
179+
180+
To make this a production-ready application:
181+
182+
1. **Install via Composer**: Run full Laravel installation
183+
2. **Environment**: Generate APP_KEY with `php artisan key:generate`
184+
3. **Database**: Run migrations with `php artisan migrate --seed`
185+
4. **Assets**: Set up Vite for asset compilation
186+
5. **Middleware**: Add CSRF protection middleware
187+
6. **Validation**: Add form request classes
188+
7. **Testing**: Implement PHPUnit tests
189+
8. **Caching**: Configure Redis/Memcached
190+
9. **Queue**: Set up queue workers for PostHog events
191+
10. **Deployment**: Configure for production server
192+
193+
## 📝 Notes
194+
195+
- This implementation uses inline CSS (matching Flask example) instead of Tailwind compilation
196+
- Livewire provides reactivity without separate JavaScript files
197+
- PostHog service is dependency-injected into components/controllers
198+
- Manual error capture pattern matches Flask implementation
199+
- Session-based burrito counter (same as Flask)
200+
- Default admin account: [email protected] / admin
201+
202+
## 🎓 Learning Resources
203+
204+
- [Laravel Documentation](https://laravel.com/docs)
205+
- [Livewire Documentation](https://livewire.laravel.com)
206+
- [PostHog PHP SDK](https://github.com/PostHog/posthog-php)
207+
- [Eloquent ORM](https://laravel.com/docs/eloquent)
208+
209+
---
210+
211+
**Implementation Date**: January 2026
212+
**Laravel Version**: 11.x
213+
**Livewire Version**: 3.x
214+
**PostHog PHP SDK**: 3.x

0 commit comments

Comments
 (0)