Tiny is a lightweight PHP framework designed to provide a simple and efficient way to build web applications with minimal setup and configuration. It was developed by Ran Aroussi for internal projects and offers a range of features to streamline development while maintaining flexibility.
- PHP 8.3+ support
- Routing system
- MVC architecture
- Database abstraction
- Caching with APCu or Memcached
- Component-based views
- Middleware support
- Environment-based configuration
- Job/task Scheduler
- Utility functions for everyday tasks
- Migration system
- Deployment using
git push
- Create a new directory for your project.
$ mkdir my-project && cd my-project
- Clone the repository or download the Tiny framework files.
$ git clone https://github.com/ranaroussi/tiny.git
- Execute the following command to create the project:
$ php tiny/cli create
- Install dependencies:
$ composer install
The resulting directory structure should look like this:
/your-project
├── app
│ ├── controllers
│ ├── models
│ ├── views
│ │ ├── components
│ │ └── layouts
│ └── middleware
│ └── middleware.php
├── html (public directory)
│ └── index.php
├── migrations
├── tiny
├── vendor
├── .env.example
├── composer.json
└── env.php
-
Edit the
.env.local
file to match your environment (rename it to.env.prod
for production, or use environment variables). -
Set up your web server to point to the
html
directory as the document root.
Tiny uses environment variables for configuration. You can set these in your .env.*
files or directly in your server environment.
Key configuration options:
DB_TYPE
: Database type (mysql, postgresql, sqlite)DB_HOST
,DB_PORT
,DB_NAME
,DB_USER
,DB_PASS
: Database connection detailsMEMCACHED_HOST
,MEMCACHED_PORT
: Memcached connection details (if using Memcached)AUTOLOAD_HELPERS
: Comma-separated list of helpers to autoload
Tiny uses a simple routing system based on the URL structure. Controllers are automatically mapped based on the URL path.
For example:
/
maps toapp/controllers/home.php
/users
maps toapp/controllers/users.php
/users/profile
maps toapp/controllers/users/profile.php
Controllers handle the logic for each route. They should be placed in the app/controllers
directory.
Example controller:
<?php
class Users extends TinyController
{
public function get($request, $response)
{
// Handle GET request
$response->render('users/index');
}
public function post($request, $response)
{
// Handle POST request
}
}
Views are PHP files that handle the presentation layer. They should be placed in the app/views
directory.
Tiny supports components and layouts for reusable view elements:
<?php
Component::render('header', ['title' => 'Home']);
?>
<h1>Hello, world</h1>
<p>This is the defalut home page</p>
<?php
Component::render('footer');
?>
Models handle data and business logic. They should be placed in the app/models
directory.
Example model:
<?php
class UserModel extends TinyModel
{
public function getUsers()
{
return tiny::db()->query("SELECT * FROM users");
}
}
Middleware can perform actions before or after request processing. To use it, place the middleware files in the app/middleware
directory.
$ ls -la /app/middleware
$ auth.php some-other-middleware.php
Make sure that middleware files include a class name with the file's name and the Middleware
suffix (for example, auth.php
should have a class named AuthMiddleware
) and a function named handle()
:
<?php
class AuthMiddleware
{
public function handle() {
// this method will be called by tiny upon initialization
}
// ...
}
Finally, edit /app/middleware.php
and add the middleware you want to use.
<?php
tiny::middleware('auth');
tiny::middleware('some-other-middleware');
//...
Tiny provides various helper functions for common tasks. You can autoload helpers by listing them in the AUTOLOAD_HELPERS
environment variable.
Tiny supports MySQL, PostgreSQL, and SQLite databases. Configure your database connection in the .env.*
file.
To query the database:
$results = tiny::db()->getQuery("SELECT * FROM users WHERE id = 1");
You can also use Tiny's utility function to build queries using placeholders:
$query = tiny::db()->prepare("SELECT * FROM users WHERE id = ? AND is_deleted = ?", [$userId, false]);
$results = tiny::db()->getQuery($query);
Tiny uses APCu or Memcached for caching. To use the cache:
$value = tiny::cache()->get('key');
tiny::cache()->set('key', $value, 3600); // Cache for 1 hour
If you need to share variables across modules, you can use the tiny::data()
method:
// controller
tiny::data()->site_name = 'My Site';
// or
tiny::set('site_name', 'My Site');
// view
<h1><?php echo tiny::data()->site_name; ?></h1>
// or
<h1><?php echo tiny::get('site_name'); ?></h1>
It is recommended that only the /html
directory be exposed to the world and that /tiny
, /app
, and /vendor
be kept in the parent directory.
/home/webapp/html (exposed to public)
/home/webapp/tiny
/home/webapp/app
/home/webapp/vendor
/home/webapp/.env.prod
/home/webapp/composer.json
/home/webapp/env.php
For information on deploying using git push
, see GIT-DEPLOY.md.
[!WARNING] Intelephense users Intelephense users might see a warning about
Undefined type 'tiny'
. This is because thetiny
main class is registered at runtime, and Intelephense isn't aware of it. You can safely ignore the message as the code will work just fine. However, if you want to get rid of this message, simply adduse Tiny\tiny;
to the beginning of your file.
Tiny includes a simple migration system to help manage database schema changes. The migration system uses a SQLite database to track which migrations have been run.
To create a new migration, use the following command:
php tiny/cli migration create migration_name
This will create a new migration file in the migrations
directory with a timestamp prefix.
To run all pending migrations:
php tiny/cli migration up
This command will execute all migrations that haven't been run yet.
To roll back the last batch of migrations:
php tiny/cli migration down
This command will revert the most recent batch of migrations.
To remove a migration file that hasn't been run yet:
php tiny/cli migration remove migration_name
This command will remove the specified migration file if it hasn't been applied to the database.
Each migration file should contain an up
method for applying the migration and a down
method for reverting it. Here's an example:
<?php
class CreateUsersTable
{
private PDO $db;
public function __construct()
{
$this->db = tiny::db()->getPdo();
}
public function up(): void
{
$this->db->execute("
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
");
}
public function down(): void
{
$this->db->execute("DROP TABLE IF EXISTS users");
}
}
The $this->db
parameter is an instance of the database connection, allowing you to execute SQL statements or use the query builder methods.
- Keep migrations small and focused on a single change.
- Use descriptive names for your migrations (e.g.,
create_users_table
,add_email_to_users
). - Always provide a
down
method that reverts the changes made in theup
method. - Test your migrations thoroughly, especially the
down
method, before applying them to production.
Tiny PHP Framework is distributed "As is" under the Apache 2.0 License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.