Lestah
128
1
Laravel

Single Action Controller

Posted 1 month ago by Lestah

I'm just trying to convert a closure-based routes to use a single action controller this is my example code

on my routes.php

// Route::get('themes/{file?}', function ($file = null) {
//     $path = base_path(config('path.themes', 'themes').'/'.settings('active_theme', 'default'))."/$file";
//     $fileArray = explode('/', $file);
//     $lastFile = end($fileArray);
//     $extension = explode(".", $lastFile);
//     $fileExtension = end($extension);
//     $isCss = 'css' === $fileExtension ? true : false;

//     if (! in_array($fileExtension, config('downloadables', []))) {
//         return abort(403);
//     }

//     if (\File::exists($path)) {
//         $headers = [
//             'Cache-Control' => 'public',
//             'Content-Type' => 'text/css'
//         ];
//         return response()->file($path, $isCss ? $headers : []);
//     }

//     return abort(404);
// })->where('file', '.*');

Route::get('theme/{file?}', 'FuzzyController')->where('file', '.*')->name('fuzzy-theme');

and on my Controller


<?php

namespace App\Http\Controllers;
namespace Core\Controllers\Assets;

// use App\User;
use App\Http\Controllers\Controller;

class FuzzyController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return View
     */
    public function __invoke(Request $request, $file = null)
    {
        $path = base_path(config('path.themes', 'themes').'/'.settings('active_theme', 'default'))."/$file";
        $fileArray = explode('/', $file);
        $lastFile = end($fileArray);
        $extension = explode(".", $lastFile);
        $fileExtension = end($extension);
        $isCss = 'css' === $fileExtension ? true : false;

        if (! in_array($fileExtension, config('downloadables', []))) {
            return abort(403);
        }

        if (\File::exists($path)) {
            $headers = [
                'Cache-Control' => 'public',
                'Content-Type' => 'text/css'
            ];
            return response()->file($path, $isCss ? $headers : []);
        }

        return abort(404);
        // return view('user.profile', ['user' => User::findOrFail($id)]);
    }

}


then i get this error when running app:serve

Uncaught UnexpectedValueException: Invalid route action: [AssetsController].

can someone explain why i get this invalid route action thanks :D

Please sign in or create an account to participate in this conversation.