ThinkPHP Documentation

repository·8.x·Indexed 26 days ago

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

A high-performance PHP framework for rapid web application development. Version 8 is optimized for PHP 8.0+, emphasizing modern PSR standards, PSR-2 coding style, PSR-4 autoloading, and enhanced debugging via the think-dumper service. Features include a built-in development server, think-orm 3.0+, and a flexible routing system using the Route facade.

Tokens
1.3K
Snippets
6
Records
9
Agent score
93%

What's inside ThinkPHP

  1. ThinkPHP 8 System Requirements and Features

    8.x

    ThinkPHP 8 is refactored for PHP 8.0+ and includes the following key characteristics:

    • Runtime Requirement: PHP 8.0 or higher.
    • PSR Compliance: Upgraded PSR dependencies.
    • ORM: Depends on think-orm version 3.0+.
    • Debugging: Includes the new think-dumper service which supports remote debugging.
    • Upgradability: Supports seamless upgrades 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 initialize a new directory (named tp in the example) with the ThinkPHP framework structure.

    composer create-project topthink/think tp
  3. Update the ThinkPHP framework

    8.x

    To update the framework to the latest version within an existing project, use the composer update command targeting the topthink/framework package.

    composer update topthink/framework
  4. 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
  5. Configure main application settings in config/app.php

    8.x

    The config/app.php file defines the core behavior of the ThinkPHP application. You can modify these keys to control namespaces, routing, timezones, and error handling.

    Key configuration options include:

    • app_namespace: The application's base namespace.
    • with_route: Boolean to enable or disable routing (true to enable).
    • default_app: The default application name (e.g., 'index').
    • default_timezone: The default timezone for the application (e.g., 'Asia/Shanghai').
    • app_map: Mapping for automatic multi-application mode.
    • domain_bind: Domain binding for automatic multi-application mode.
    • deny_app_list: A list of applications that are prohibited from URL access.
    • exception_tmpl: The template file path for exception pages.
    • error_message: The error message displayed when not in debug mode.
    • show_error_msg: Boolean to determine if error messages should be displayed (set to false for production environments).
    return [
        'app_namespace'    => '',
        'with_route'       => true,
        'default_app'      => 'index',
        'default_timezone' => 'Asia/Shanghai',
        'app_map'          => [],
        'domain_bind'      => [],
        'deny_app_list'    => [],
        'exception_tmpl'   => app()->getThinkPath() . 'tpl/think_exception.tpl',
        'error_message'    => '页面错误!请稍后再试~',
        'show_error_msg'   => false,
    ];
  6. Define application logic in a Controller

    8.x

    In ThinkPHP, controllers are classes that extend BaseController (or a base class within your application namespace). Each public method within the controller class represents an action that can be mapped to a URL route. Methods can accept parameters, which are automatically injected from the request.

    namespace app\controller;
    
    use app\BaseController;
    
    class Index extends BaseController
    {
        /**
         * A standard action method
         */
        public function index()
        {
            return 'Welcome to ThinkPHP';
        }
    
        /**
         * An action method with a default parameter
         * @param string $name
         */
        public function hello($name = 'ThinkPHP8')
        {
            return 'hello,' . $name;
        }
    }
  7. Define HTTP GET routes using the Route facade

    8.x

    Use the think\facade\Route facade to define application routes. You can map a URL pattern to either a closure (anonymous function) or a controller method string.

    Route to a Closure

    To return a direct response from a route, pass a closure as the second argument to Route::get().

    Route to a Controller

    To map a route to a controller method, pass a string in the format 'controller/method' as the second argument. You can use dynamic parameters in the URL pattern using the /:parameter syntax.

    use think\facade\Route;
    
    // Route returning a string via closure
    Route::get('think', function () {
        return 'hello,ThinkPHP8!';
    });
    
    // Route with a dynamic parameter mapping to a controller method
    Route::get('hello/:name', 'index/hello');