ThinkPHP Framework Documentation

repository·8.x·Indexed 24 days ago

https://github.com/top-think/framework

A high-performance PHP framework for rapid web application development. Version 8 is optimized for PHP 8.0+ and features upgraded PSR dependencies, think-orm 3.0+, and think-dumper for remote debugging. The documentation covers installation via Composer, application initialization, service container management, cache services, and a comprehensive suite of CLI 'make' commands for scaffolding controllers, models, middleware, and other framework components.

Tokens
8.9K
Snippets
3
Records
112
Agent score
84%

What's inside ThinkPHP

  1. ThinkPHP 8 System Requirements and Features

    8.x

    ThinkPHP 8 is a refactored version of the framework designed for modern PHP environments.

    Requirements:

    • PHP version: 8.0+

    Key Features:

    • Refactored based on PHP 8.0+.
    • Upgraded PSR dependencies.
    • Depends on think-orm version 3.0+.
    • New think-dumper support for remote debugging.
    • Provides a seamless upgrade path from versions 6.0 and 6.1.
  2. Install ThinkPHP 8 via Composer

    8.x

    To create a new ThinkPHP project, use the composer create-project command. This will scaffold a new directory (named tp in the example below) with the framework installed.

    composer create-project topthink/think tp
  3. Start the ThinkPHP development server

    8.x
    After installing the project, navigate to your project directory and use the php think run command to start the built-in development server. The service will typically be available at http://localhost:8000.
    cd tp
    php think run
  4. Manage named routes with RuleName

    8.x
    The think\route\RuleName class is used to manage route identifiers, route groups, and specific routing rules within the ThinkPHP framework. It allows you to register named routes for URL generation, manage route groups, and retrieve route information based on patterns or identifiers.
  5. Initialize the ThinkPHP Application

    8.x
    To start the ThinkPHP framework, instantiate the think\App class and call the initialize() method. This process loads environment variables, configuration files, language packs, and triggers the AppInit event. It also sets the default timezone based on the app.default_timezone configuration.
  6. Configure Route settings

    8.x

    The Route class uses a configuration array that can be customized via the application's route config file. Key configuration options include:

    KeyDescription
    url_lazy_routeWhether to enable lazy route parsing (boolean).
    url_route_mustWhether to force the use of routing (boolean).
    url_case_sensitiveWhether route matching is case-sensitive (boolean).
    route_rule_mergeWhether to merge route rules (boolean).
    route_complete_matchWhether routes must match the URL completely (boolean).
    remove_slashWhether to remove trailing slashes from URLs (boolean).
    default_route_patternThe default pattern for route variables (string).
    url_html_suffixThe URL pseudo-static suffix (e.g., 'html').
    controller_layerThe name of the controller layer (string).
    default_moduleThe default module name (string).
    default_controllerThe default controller name (string).
    default_actionThe default action name (string).
    api_versionThe header variable used for API versioning (string).
  7. Generate URL by route name using [name] syntax

    8.x
    To generate a URL based on a named route, pass the route name wrapped in square brackets [name] as the URL string in the Url constructor. This tells the builder to look up the route definition by its identifier rather than treating the string as a literal path.
  8. Register view and redirect routes

    8.x

    ThinkPHP provides specialized methods for direct view rendering and URL redirection within the routing layer.

    • view(string $rule, string $template, array $vars): Maps a URL pattern directly to a view template.
    • redirect(string $rule, string $route, int $status): Redirects a URL pattern to a new location with a specific HTTP status code (default 301).
  9. Regenerate session ID in Store

    8.x

    Use the regenerate(bool $destroy = false) method to change the current session ID. This is useful for preventing session fixation attacks.

    • If $destroy is set to true, the old session data is deleted from the handler before the new ID is generated.
    • If $destroy is false, a new ID is generated but the current data remains in the session.