Laravel5.2+Maatwebsite Excel2.0.8导出异常:exportInventory无法访问
Let’s break down the most likely causes for your "cannot access this page" error when using InventoryRecordController@exportInventory, especially since your SaleController export works perfectly. I’ll walk through step-by-step checks tailored to your code and XAMPP 7.2.12 environment:
1. First, Verify Your Route Configuration
The #1 reason for "cannot access page" errors in Laravel is a missing or misconfigured route. Compare your route for exportInventory to the working exportSales route:
- Ensure the route allows the optional
$paramparameter with a?in the URI. For example:// Correct syntax for optional parameter Route::get('/inventory/export/{param?}', 'InventoryRecordController@exportInventory'); - If you forgot the
?, Laravel will throw a 404 when you access the route without a parameter. - Double-check the HTTP method (GET/POST) matches how you’re accessing the route—most export functions use GET.
2. Confirm the Inventory View File Exists & Is Accessible
Your exportInventory method loads inventory/view_inventory_excel—make sure:
- The file exists at
resources/views/inventory/view_inventory_excel.blade.php(check spelling and folder structure; even Windows/XAMPP can fail if case doesn’t match on some setups). - The view has no syntax errors (e.g., missing blade directives, typos in variable names like
$inventory). A broken view can trigger a 500 error that browsers sometimes display as "cannot access page."
3. Check for Issues in the InventoryRecord Model Query
Your working Sale query is straightforward, so let’s validate the InventoryRecord query:
- Does the
InventoryRecordmodel exist in yourapp/directory with the correct namespace? - Is the model’s table name correctly defined? By default, Laravel expects
inventory_recordsfor theInventoryRecordmodel—if your table has a different name, addprotected $table = 'your_table_name';to the model. - Test the query directly in a test route or
php artisan tinkerto confirm it returns data:// Run this in a test route or via tinker $inventory = InventoryRecord::orderBy('created_at', 'desc')->get(); dd($inventory); // Should return a collection of records, not an error
If this throws an exception (e.g., table not found, column doesn’t exist), that’s the root cause.
4. Validate Output Buffer Handling
Both controllers use ob_end_clean() and ob_start(), but residual output can break Excel exports:
- Ensure there’s no accidental output in the
InventoryRecordControllerconstructor, middleware, or any code that runs beforeexportInventory(e.g.,echo,var_dump, or even whitespace outside of<?php ?>tags in your model/controller files). - Try moving
ob_end_clean()andob_start()to the very top of the method, before any other logic, to ensure no output is sent before the Excel response.
5. Check PHP & Laravel Error Logs
"Cannot access page" often hides a 500 server error. Look for detailed errors in:
- XAMPP PHP Log:
xampp/php/logs/php_error.log(check for fatal errors like missing extensions or syntax issues). - Laravel Log:
storage/logs/laravel.log(look for exceptions related to Excel, database, or views).
Common culprits here are missing PHP extensions required by Maatwebsite Excel (e.g.,php_zip,php_xml,php_gd2—ensure these are enabled in yourphp.inifile).
6. Test a Minimal Export to Isolate the Problem
To rule out view/query issues, replace your exportInventory method with a minimal working example:
public function exportInventory( $param = null ){ ob_end_clean(); ob_start(); Excel::create('InventoryTest', function($excel){ $excel->sheet('test_sheet', function($sheet){ // Simple static data to test export functionality $sheet->fromArray([ ['Product', 'Quantity'], ['Aspirin', 100], ['Paracetamol', 200] ]); }); })->download('xls'); }
If this works, the problem lies in your original view or InventoryRecord query. If it still fails, the issue is with your route, PHP environment, or Maatwebsite Excel setup.
内容的提问来源于stack exchange,提问作者Eli

