Quick Start Guide
This guide will walk you through creating your first route, controller, and view in the TinyMVC framework.
Or, just run the following command:
composer create-project tinymvc/tinymvc myapp
1. Create Your First Route
Routes define the entry points to your application. Let's create a simple route that welcomes users by name.
Open the file routes/web.php
and add:
<?php
use Spark\Http\Route;
use App\Http\Controllers\HomeController;
// Simple GET route with a name parameter
Route::get('/hello/{name}', [HomeController::class, 'index']);
{name}
in the URL is a dynamic parameter that
will be passed to your controller.
2. Generate a Controller
Controllers handle the application logic. Let's create one using the Spark CLI:
php spark make:controller HomeController
This will create a new controller at app/Http/Controllers/HomeController.php
.
Edit the controller to add our welcome method:
<?php
namespace App\Http\Controllers;
class HomeController extends Controller
{
/**
* Welcome a user by name
*
* @param string $name The name to welcome
* @return \Spark\Http\Response
*/
public function index(string $name)
{
return view('greet', ['name' => $name]);
}
}
- Method parameters are automatically injected from route parameters
- The
view()
helper renders templates with the provided data
3. Create a View
Views handle the presentation layer. Let's generate and customize one:
First, create the view file using Spark CLI:
php spark make:view greet
This creates resources/views/greet.blade.php
. Edit it to display our welcome message:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to My App</title>
</head>
<body>
<h1>Welcome, {{ $name }}!</h1>
@if ($name === 'admin')
<p class="alert">Special message for admin users</p>
@endif
</body>
</html>
- Always escape output with
{{ }}
or{{ __() }}
to prevent XSS - Keep complex logic in controllers, not views
- Use partials for reusable view parts. (header, footer)
- Use components for encapsulating reusable UI elements
4. Test Your Application
Start the development server:
php spark serve
Visit your route in the browser:
http://localhost:8000/hello/YourName
You should see the welcome message with your name!
Next Steps
Now that you have a basic route working, consider exploring:
- Advanced Routing - Middleware, route groups, etc.
- Database Integration - Connect to a database
- Authentication - User login system
- Asset Compilation - CSS/JavaScript bundling
Troubleshooting
If you encounter issues:
Issue | Solution |
---|---|
Route not found | Check routes/web.php exists and has correct syntax |
Controller not found | Verify namespace in controller matches file location |
View not found | Ensure view file exists in resources/views/ |
Server not starting | Check if port 8000 is available or specify another:
php spark serve --port=8080
|
php spark route:list
to see all registered routes
with their name, and allowed methods.