I’m trying to get my app to download an Excel file when clicking the button. I can’t seem to get past this 404 route error.
Route
Route::get('/exportPriceList', [ProductController::class, 'exportPriceList'])->name('products.exportPriceList');
Controller
use IlluminateHttpRequest;
use IlluminateSupportFacadesStorage;
class ProductController extends Controller
{
public function exportPriceList(Request $request)
{
$price_list = Storage::download('/documents/price-list.xlsx');
return response()->download($price_list);
}
}
Blade/View
<li class="nav-item">
<a href="/exportPriceList" class="btn btn-primary">
<i class="fas fa-download">
</i>Download Price List</a>
</li>
Update:
Although the /exportPriceList gave me nothing. Calling the route gave me a new error to chase….
Class "BrandController" does not exist
at vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:223
219▕ if ($this->isFrameworkController($route)) {
220▕ return false;
221▕ }
222▕
➜ 223▕ $path = (new ReflectionClass($route->getControllerClass()))
224▕ ->getFileName();
225▕ } else {
226▕ return false;
227▕ }
+3 vendor frames
4 [internal]:0
IlluminateFoundationConsoleRouteListCommand::IlluminateFoundationConsole{closure}(Object(IlluminateRoutingRoute))
+16 vendor frames
21 artisan:35
IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
ANSWER!!!
Ohhh man that was weird but I learned some cool new tools
php artisan route:clear
php artisan route:cache
or, if you run into some other file location or view errors, just run
$>php artisan optimize:clear
worked perfectly…
2
Answers
unsure as why but... ran these and then all the routes worked!!!
?> php artisan route:clear then ?> php artisan route:cache
Just use the route function to generate the URL based on the route name. Modify your blade code like this: