frostybee/slim-mvc
最新稳定版本:v1.3.0
Composer 安装命令:
composer create-project frostybee/slim-mvc
包简介
An MVC-based web application project template that uses the Slim PHP microframework.
README 文档
README
A lightweight MVC web application starter template built on top of the Slim PHP microframework. Ideal for projects that require Slim's simplicity without sacrificing the benefits of a clean MVC architecture.
Why Using this Template?
This template provides a starting point for building web applications with the Slim 4 framework using the classic MVC (Model–View–Controller) pattern. It includes everything you need to get started, without the extra complexity of larger frameworks.
What's Included
This starter template follows best practices and adheres to industry standards:
- Slim 4: The "slim" PHP microframework
- Routing: Slim's custom routing based on FastRoute
- Dependency injection container (PSR-11)
- HTTP message interfaces (PSR-7)
- HTTP Server Request Handlers, Middleware (PSR-15)
- Autoloader (PSR-4)
- Logger (PSR-3)
- Code styles (PSR-12)
- Composer - Dependency management
Requirements
- PHP 8.2 or higher
- Composer (for dependency management)
- A web server (Apache, Nginx)
How Do I Use/Deploy this Template?
- Option 1: Using Composer (Recommended)
- Option 2: Using Docker (macOS/Linux/Windows)
- Option 3: Manual Installation
Option 1: Using Composer (Recommended)
Prerequisites:
- PHP 8.2 or higher installed locally
- Composer installed globally
- A web server (Apache/Nginx) or Wampoon
Don't have a WAMP/LAMP stack installed? Use Option 2: Docker instead.
- Open a terminal in your web server's document root (i.e.,
htdocs). - Run the following command:
composer create-project frostybee/slim-mvc [project-name]-app
Replace[project-name]with your project name (e.g.,worldcup-app). - Open your
[project-name]-appfolder in VS Code. - Adjust your database credentials in
config/env.php(see below).
Option 2: Using Docker (macOS/Linux/Windows)
Docker allows you to run the application in containers without installing PHP, Apache, or MariaDB locally. This works on macOS, Linux, and Windows.
Prerequisites:
- Install Docker Desktop
- Install Git
Quick Start:
-
Clone the repository (since Composer requires PHP, which you may not have installed):
git clone https://github.com/frostybee/slim-mvc.git [project-name]-app
Replace
[project-name]with your project name (e.g.,worldcup-app). -
Navigate to the project folder:
cd [project-name]-app -
Remove the
.gitfolder to start fresh with your own repository:rm -rf .git
-
Start all containers by executing the following command:
docker-compose up -d
Note: Use
docker-compose up -d --buildto rebuild images after modifying the Dockerfile or related configurations. -
Access the application:
- App: http://localhost:8080
- phpMyAdmin: http://localhost:8081
Configuring the Database:
The default database name is slim_mvc. To use a different database name:
-
Update
docker-compose.yml:db: environment: MYSQL_DATABASE: your_database_name
-
Update
config/env.docker.php:$settings['db']['database'] = 'your_database_name';
-
Rebuild containers:
docker-compose up -d --build
Importing Database Schema:
To automatically import a database schema when the container starts:
- Place your
.sqlfile(s) in thedocker/init-db/folder - Start the containers:
docker-compose up -d
The SQL files will be executed automatically on first container creation. If you have multiple files, they run in alphabetical order (e.g., 01-schema.sql, 02-data.sql).
To re-import the schema, remove the database volume and restart:
docker-compose down -v docker-compose up -d
Database Credentials (for phpMyAdmin):
| Username | Password |
|---|---|
| root | secret |
| slim_user | slim_pass |
Common Commands:
| Action | Command |
|---|---|
| Start containers | docker-compose up -d |
| Stop containers | docker-compose down |
| View app logs | docker-compose logs -f app |
| Run composer | docker-compose exec app composer install |
| Delete database | docker-compose down -v |
| Rebuild containers | docker-compose up -d --build |
Working with Multiple Projects:
If you run one project at a time, no configuration changes are needed. Simply stop one project before starting another:
# Stop current project docker-compose down # Switch to another project cd ../other-project docker-compose up -d
If you need to run multiple projects simultaneously, change the port numbers in docker-compose.yml to avoid conflicts:
services: app: ports: - "8082:80" # Change 8080 to 8082, 8083, etc. db: ports: - "3307:3306" # Change 3306 to 3307, 3308, etc. phpmyadmin: ports: - "8083:80" # Change 8081 to 8083, 8084, etc.
Option 3: Manual Installation
- Download this repository as a
.zipfile. - Extract the downloaded
slim-mvc-main.zipfile locally. - Copy the
slim-mvc-mainfolder into your web server's document root (i.e.,htdocs). - Rename the
slim-mvc-mainfolder to[project_name]-app(for example,worldcup-app). - Open your
[project_name]-appfolder in VS Code. - Install the project dependencies by running composer. If you are using Wampoon, open a terminal window in VS Code (hit
Ctrl+`) then run.\composer.bat update- If you are not using Wampoon to develop your app, just run composer from the command line.
- In the
configfolder, make a copy ofenv.example.phpand rename it toenv.php. - Adjust your database credentials (see below).
NOTE: You can always clone this repository. However, if you do, you need to remove the .git hidden directory before you copy this template over to htdocs
How Do I Configure My Database Connection?
Follow the outlined instructions in config/env.example.php
- Change the value of the
databasevariable to reflect the name of the database to be used by your slim app. - You may also want to change the connection credentials in that file.
How do I Use Composer with Wampoon?
To install or update your project dependencies deployed on Wampoon, use the composer.bat script as follows:
| Action | Command | Description |
|---|---|---|
| Install dependencies | ./composer.bat install |
Installs packages listed in composer.json and creates the vendor directory. |
| Update dependencies | ./composer.bat update |
Refreshes all packages to the latest versions allowed by composer.json. |
| Add a package | ./composer.bat require [package] |
Installs a new package and adds it to composer.json. |
| Regenerate autoloader | ./composer.bat dump-autoload -o |
Rebuilds the optimized autoloader after adding or removing classes. |
On Using Environment Variables
Sensitive information used in app such as your database credentials, API key, etc. MUST not be pushed into your Git repo.
Do not use .env files for storing environment specific application settings/configurations. Dotenv is not meant to be used in production
Just Google: "DB_PASSWORD" filetype:env Alternatively, you can visit the following link: Google env search
Instead, follow the instructions that are detailed in config/env.example.php
Project Structure
Here's how everything is organized:
slim-mvc/
├── app/
│ ├── Controllers/ # Your controllers live here
│ ├── Domain/ # Domain logic and business rules
│ │ ├── Models/ # Data models and entities
│ │ └── Services/ # Business logic services
│ ├── Helpers/ # Utility classes and helpers
│ ├── Middleware/ # Custom middleware
│ ├── Routes/ # Route definitions (web & API)
│ └── Views/ # Your view templates
├── config/ # Configuration files and bootstrap
├── data/ # Database files, uploads, etc.
├── docker/ # Docker configuration files
│ ├── apache.conf # Apache virtual host config
│ └── init-db/ # Database initialization scripts
├── docs/ # Documentation
├── public/ # Web-accessible files
│ ├── assets/ # Static assets (CSS, JS, images)
│ │ ├── css/ # Stylesheets
│ │ └── js/ # JavaScript files
│ ├── index.php # Application entry point
│ └── .htaccess # Apache rewrite rules
├── var/ # Runtime files
│ └── logs/ # Application logs
├── vendor/ # Composer dependencies
├── Dockerfile # Docker image definition
└── docker-compose.yml # Docker services configuration
Quick Development Tips
Adding Routes
Routes are defined in the app/Routes/ directory. Check out the existing route files to see how it's done.
Creating Controllers
Controllers go in app/Controllers/. They should extend the base controller class and follow PSR-4 autoloading.
Views and Templates
Templates are stored in app/Views/. The template engine is already configured and ready to use.
Configuration
App configuration lives in config/. Modify these files to customize your application settings.
Logging
Logs are written to the var/logs/ directory. Use the injected logger in your controllers to track what's happening.
Need Help?
- Check out the Slim documentation for framework-specific questions.
- Look at the example controllers and routes to see how everything fits together.
- The code is pretty well commented, so don't hesitate to explore it well.
Contributing
Got ideas for improvements? Found a bug? Pull requests are welcome!
Acknowledgments
The application's bootstrap process and structure of this starter template is based on slim4-skeleton by @odan. Many thanks to the original developers for their work!
License
This project is open-sourced under the MIT License. See the LICENSE file for the full details.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-15