如何修改phpgrid在Slim Framework中的编辑提交路由?
Absolutely, you can reroute phpGrid's edit and create form submissions to your Slim controller instead of the default edit.php—and since you're working with an API-driven workflow (no direct DB connection), here's a tailored step-by-step guide to make this work:
When initializing your phpGrid instance, use the editurl option to point directly to your Slim controller endpoint. This tells phpGrid where to send all add/edit/delete requests instead of its built-in edit.php.
Here's how to configure it:
$grid = new jqGrid(); // Your existing configuration (columns, data source setup, etc.) $grid->set_col_names(['ID', 'Name', 'Email']); // ... other column configs ... // Set the custom submit URL to your Slim route $grid->set_options([ 'editurl' => '/grid/handle-submissions', // Match your Slim route path 'datatype' => 'json', // Critical since you're using API data 'mtype' => 'POST' // Use POST for form submissions ]);
Create a Slim route that accepts the POST requests from phpGrid. phpGrid sends specific parameters with each request:
oper: The action being performed (add,edit, ordel)id: The row ID (for edit/delete operations)- Field values: Your grid's column names paired with submitted data
Here's a sample controller implementation:
// Add this to your Slim app setup $app->post('/grid/handle-submissions', function ($request, $response) { // Get the submitted form data $submittedData = $request->getParsedBody(); // Extract core operation details $operation = $submittedData['oper'] ?? ''; $rowId = $submittedData['id'] ?? ''; // Filter out phpGrid's internal parameters to get your actual form fields $formFields = array_filter($submittedData, function ($key) { return !in_array($key, ['oper', 'id', '_search', 'nd', 'rows', 'page', 'sidx', 'sord']); }, ARRAY_FILTER_USE_KEY); // Handle each operation with your API switch ($operation) { case 'add': // Call your API to create new data $apiResult = $this->yourApiClient->createRecord($formFields); break; case 'edit': // Call your API to update the existing record $apiResult = $this->yourApiClient->updateRecord($rowId, $formFields); break; case 'del': // Call your API to delete the record $apiResult = $this->yourApiClient->deleteRecord($rowId); break; default: return $response->withJson(['success' => false, 'message' => 'Invalid operation'], 400); } // Return the response phpGrid expects if ($apiResult['success']) { return $response->withJson(['success' => true]); } else { return $response->withJson([ 'success' => false, 'message' => $apiResult['error'] ?? 'Failed to process request' ], 400); } });
Since you're fetching data via API instead of a direct DB connection, make sure your phpGrid is configured to pull data from a Slim endpoint too:
$grid->set_options([ 'url' => '/grid/fetch-data', // Slim route that returns your API data 'datatype' => 'json', 'ajaxGridOptions' => [ // Optional: Add auth headers if your API requires it 'headers' => ['Authorization' => 'Bearer ' . $_SESSION['auth_token']] ] ]);
Then create the corresponding Slim route to serve data to phpGrid:
$app->get('/grid/fetch-data', function ($request, $response) { // Fetch data from your API $apiData = $this->yourApiClient->getGridData(); // Format data to match phpGrid's expected JSON structure $gridResponse = [ 'page' => 1, // Adjust based on pagination needs 'total' => 1, // Total number of pages 'records' => count($apiData), // Total number of records 'rows' => $apiData // Your actual data rows ]; return $response->withJson($gridResponse); });
- Response Format: phpGrid relies on a specific JSON response format. Always return
{"success": true}for successful operations, or{"success": false, "message": "Error details"}for failures. - Request Headers: If your API uses authentication (like JWT), use
ajaxGridOptionsandajaxEditOptionsin phpGrid to attach the necessary headers to all requests. - Debugging: Use your browser's DevTools to inspect network requests—check that the
editurlis correct, parameters are being sent, and your Slim controller is returning the right response.
内容的提问来源于stack exchange,提问作者Ares Draguna

