You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求支持JSON输出的PHP MySQL类库适配Vue+Axios项目

Lightweight PHP Libraries for MySQL with JSON Responses (Alternative to Custom Queries & Laravel)

Hey Joseph, totally get where you're coming from—building a Vue/Axios frontend means you need clean, easy-to-generate JSON from your PHP backend, and spinning up a full Laravel app feels like overkill when you just need reliable database handling. Here are some solid, lightweight options that fit your needs perfectly:

1. Standalone Eloquent ORM

If you like Laravel's Eloquent syntax but don't want the entire framework overhead, you can use Eloquent on its own. It's a powerful ORM that simplifies query writing, and converting results to JSON is dead simple.

  • Setup: Install via Composer: composer require illuminate/database
  • Example Usage:
    <?php
    require 'vendor/autoload.php';
    
    use Illuminate\Database\Capsule\Manager as Capsule;
    
    // Initialize database connection
    $capsule = new Capsule;
    $capsule->addConnection([
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'your_database',
        'username'  => 'your_username',
        'password'  => 'your_password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
    ]);
    $capsule->bootEloquent();
    
    // Define a basic model (e.g., User.php)
    class User extends Illuminate\Database\Eloquent\Model {}
    
    // Fetch data and return JSON
    header('Content-Type: application/json');
    $activeUsers = User::where('active', 1)->get();
    echo $activeUsers->toJson();
    
  • Why it works: Eloquent collections have a built-in toJson() method, and setting the Content-Type header ensures Axios parses the response correctly without extra frontend work.

2. Medoo

Medoo is a super lightweight, beginner-friendly database library built for simplicity. It supports MySQL and other databases, and since it returns plain PHP arrays, converting to JSON is trivial.

  • Setup: Install via Composer: composer require catfan/medoo
  • Example Usage:
    <?php
    require 'vendor/autoload.php';
    
    use Medoo\Medoo;
    
    // Initialize connection
    $db = new Medoo([
        'database_type' => 'mysql',
        'database_name' => 'your_database',
        'server' => 'localhost',
        'username' => 'your_username',
        'password' => 'your_password'
    ]);
    
    // Fetch data and return JSON
    header('Content-Type: application/json');
    $userData = $db->select('users', ['id', 'name', 'email'], ['active' => 1]);
    echo json_encode($userData);
    
  • Why it works: Medoo’s query results are standard arrays, so json_encode() works flawlessly. It’s minimal, has no steep learning curve, and integrates quickly into existing projects.

3. Idiorm & Paris

Idiorm is a lightweight PDO wrapper that simplifies raw SQL queries, while Paris adds a basic ORM layer on top. Both are easy to set up and play perfectly with JSON responses.

  • Setup: Install via Composer: composer require j4mie/idiorm j4mie/paris
  • Example Usage:
    <?php
    require 'vendor/autoload.php';
    
    // Configure database connection
    ORM::configure('mysql:host=localhost;dbname=your_database');
    ORM::configure('username', 'your_username');
    ORM::configure('password', 'your_password');
    
    // Fetch data with Idiorm (raw query wrapper)
    header('Content-Type: application/json');
    $users = ORM::for_table('users')->where('active', 1)->find_many();
    $userArray = array_map(fn($user) => $user->as_array(), $users);
    echo json_encode($userArray);
    
    // Or use Paris (ORM) for cleaner model-based queries
    class User extends Model {}
    $activeUsers = User::where('active', 1)->find_many();
    echo json_encode($activeUsers->as_array());
    
  • Why it works: Both libraries let you convert results to arrays with as_array(), which you can then encode to JSON. They’re ideal if you want a middle ground between raw SQL and a full ORM.

Quick Universal Tip

No matter which library you choose, always set the JSON response header before outputting your data:

header('Content-Type: application/json');

This ensures Axios automatically parses the response as JSON, saving you extra parsing code on the frontend.

内容的提问来源于stack exchange,提问作者Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:57:25