FastAdmin Documentation

repository·1.x-dev·Indexed 23 days ago

https://github.com/fastadminnet/fastadmin

FastAdmin is a rapid backend development framework based on ThinkPHP and Bootstrap designed for creating administrative panels and APIs. Version 1.7.0 features one-click CRUD code generation, an Auth-based permission management system, and a robust plugin architecture. It includes CLI tools for managing addons, generating API documentation via DocBlock annotations, and automating the creation of Controllers, Models, and Views based on database tables.

Tokens
8.5K
Snippets
8
Records
39
Agent score
83%

What's inside FastAdmin

  1. Overview of FastAdmin features

    1.x-dev

    FastAdmin is a rapid backend development framework built on ThinkPHP and Bootstrap. It provides a comprehensive suite of tools for building administrative interfaces and APIs.

    Core Capabilities:

    • Permission Management: An Auth-based system supporting multi-level permission inheritance, multiple roles per administrator, and data scoping (managing sub-level or personal data).
    • One-Click Code Generation: Automatically generates CRUD components including Controllers, Models, Views, JS, Language packs, Menus, and Recycle bins. It also supports generating API documentation and compressing JS/CSS assets.
    • Frontend Architecture: Built on AdminLTE and Bootstrap for responsive design (Mobile/Tablet/PC). It uses RequireJS for modular JS management and Less for styling.
    • Extensibility: Supports online installation, uninstallation, and upgrading of plugins via a plugin market.
    • Advanced UI Components: Includes support for large file chunked uploads, drag-and-drop, image compression, fixed table columns/headers, Excel export, and template rendering.
    • Third-Party Integrations:
      • Cloud Storage: Qiniu, Alibaba OSS, Tencent Cloud, and Upyun (supports chunked uploads).
      • Payments: WeChat and Alipay (including WeChat PC scan-to-pay).
      • SMS: Alibaba Cloud and Tencent Cloud.
      • Social Login: QQ, WeChat, and Weibo.
      • Rich Text Editors: Summernote and Baidu Editor.
  2. Control CRUD code generation via field suffixes

    1.x-dev

    The Crud CLI command uses specific naming suffixes on your database columns to automatically determine the correct UI component (input type) for the generated forms and the correct formatter for the generated data tables.

    By naming your columns with these suffixes, you can control whether a field appears as a switch, a date picker, a city selector, or an image uploader without manual configuration.

    Supported Suffixes and UI Mapping

    Suffix CategoryExample SuffixResulting UI Component
    Datetime_time, _date, _datetimedatetime input
    Radio_radio (on enum type)radio selection
    Checkbox_checkbox (on set type)checkbox selection
    Switch_switch (on tinyint(1) or char(1))switch toggle
    City Picker_citycitypicker
    Date Range_rangedatetimerange
    JSON List_jsonfieldlist
    Tags Input_tagtagsinput
    Image_image, _imagesImage upload/preview
    File_file, _filesFile upload/preview

    Table Formatter Logic

    The command also automatically assigns JavaScript formatters for the data table based on these suffixes. For example, fields ending in _image will automatically receive the Table.api.formatter.image formatter and appropriate event listeners.

  3. Handle relationships during CRUD generation

    1.x-dev

    When the Crud command detects relationships in the database schema, it configures the generated files to support them:

    • HasMany Relationships: The command generates an addtabs button in the table's operation column. This button opens a dialog to the related controller's index page, filtered by the current record's ID.
    • Automatic Sub-generation: If a relationship exists but the related controller does not, the command can automatically execute a nested crud command to generate the necessary files for the related table.
    • Model Methods: It generates the corresponding model relationship methods (e.g., hasOne, belongsTo, hasMany) in the generated Model file.
    • Eager Loading: For certain relationship types, it configures the Controller's index method to use ->with([...]) for eager loading to prevent N+1 query issues.
  4. Configure field comments for custom labels and options

    1.x-dev

    You can use database column comments to provide custom display labels and predefined options for select, radio, or checkbox fields during CRUD generation.

    Syntax for Options

    To define a list of options for a field, use the format FieldLabel:key1=Value1,key2=Value2. The command parses the comment to create the searchList in the JavaScript table and the selection options in the form.

    Example Comment Format: Status:1=Active,2=Inactive

    This will result in:

    1. A label Status.
    2. A search list in the table containing Active and Inactive.
    3. A selection UI in the form with those specific key-value pairs.
  5. How CRUD component mapping works via suffixes

    1.x-dev

    The crud command uses specific field name suffixes to automatically determine which UI component to generate in the views and JavaScript files. You can influence the generated UI by naming your database columns as follows:

    SuffixComponent TypeExample Field Name
    _id, _idsselectpage (Searchable Select)user_id, group_ids
    contentRich Text Editordescription_content
    switchToggle/Switchis_active_switch
    cityCity Pickeraddress_city
    rangeTime/Date Rangecreated_range
    json, arrayFieldList (JSON)config_json
    tag, tagsTag Componentcategory_tags
    timeDatetime Pickerexpire_time
    status, state, dataRadio/Checkbox (Enum/Set)order_status
    image, avatarImage Componentuser_avatar
    fileFile Componentattachment_file
  6. Automatic field type mapping in Crud generation

    1.x-dev

    The Crud command inspects database column types and comments to determine the appropriate UI components for the generated forms and tables. Supported mappings include:

    • Select/Radio/Checkbox: Uses getEnum to generate model methods for retrieving options and assigns them to the view.
    • Datetime: Generates appropriate datetime pickers.
    • Textarea: Generates standard textareas.
    • Switch: Generates a toggle switch UI.
    • Citypicker: Generates a city selection component.
    • Tagsinput: Generates a tag input component.
    • Fieldlist: Generates a specialized list-based input (array/template).
    • Selectpage: Automatically configures selectpage for fields with specific suffixes, enabling searchable dropdowns linked to other controllers.
    • Image/File: Detects image or file suffixes to generate upload components.
    • Soft Delete: If a delete_time field is detected, it automatically adds the SoftDelete trait to the Model and includes recycle bin functionality in the UI.
  7. Install FastAdmin via Web Interface

    1.x-dev

    FastAdmin provides a web-based installation wizard. When accessing the installation page via a browser, you will be prompted to provide:

    • MySQL Configuration: Hostname, Hostport, Database name, Username, and Password.
    • Table Prefix: Defaults to fa_.
    • Admin Account: Username, Password (must be 6-16 characters and not in the weak password list), and Email.
    • Site Name: The name of your website.

    Upon successful installation, the system will redirect or return a JSON response containing the adminName. This adminName represents the renamed admin entry file (e.g., randomstring.php) located in the public/ directory, which is used to access the backend securely.

  8. Install FastAdmin via CLI

    1.x-dev

    You can install FastAdmin using the install command. This command sets up the database, creates the admin user, and generates a unique admin entry file for security.

    Note: If an install.lock file already exists, the installation will fail unless you use the --force=true flag.

    Requirements:

    • PHP version 7.4.0 or higher.
    • PDO extension installed.
    • Write permissions for application/database.php and .env.
  9. Generate CRUD code via the Crud command

    1.x-dev

    The Crud command is a CLI tool used to rapidly generate a complete set of files for a database table, including Controllers, Models, Validators, and Views (Add, Edit, Index, and JavaScript files). It automatically handles field types, relationships, and language files based on the database schema.

    When generating code, the command performs the following:

    • Controller/Model/Validator Generation: Creates the core logic files.
    • View Generation: Creates HTML templates for adding and editing records.
    • JavaScript Generation: Creates the client-side logic for the data table (e.g., columns, search, and operations).
    • Language Generation: Creates language files for internationalization based on field comments.
    • Menu Generation: If requested, it automatically adds the new controller to the system menu.
    • Relationship Handling: If the table has relationships (e.g., hasmany), it can automatically generate the related controller and model files.
  10. Use the menu CLI command to manage system menus

    1.x-dev

    The menu command allows you to automatically build or delete authentication menus and permission rules based on your controller files. It scans controller classes, reads their docblock comments for metadata (like icons and remarks), and populates the AuthRule model.

    Build Menus

    To build menus for specific controllers, provide the controller names. The command will scan the files and create menu entries and permission rules.

    Delete Menus

    To remove existing menu entries, use the --delete flag. You can specify controllers to target specific menus or use all-controller to clear everything (though deleting all is restricted).

    Metadata via Docblocks

    You can control how menus appear in the UI by adding annotations to your controller class and methods:

    • @icon <font-awesome-class>: Sets the menu icon (e.g., @icon fa fa-user).
    • @remark <text>: Sets the remark/description for the menu item.
    • @internal: If this tag is present in the class or method docblock, the command will ignore it and not create a menu entry.

    Note: Method titles in the menu are derived from the method name (e.g., index becomes Index) unless a docblock comment is provided for that method.