frankot
frankot

Reputation: 190

Laravel can not import class from correct file, it looks in controllers

I tried to separate additional classes into files gathered in the App\Core directory. I have already tried different solutions for the problem and nothing has worked for me. I managed to create one test class static SearchProcess placed in the SearchProcess.php file. So the full path looks like App\Core\SearchProcess.php.

The file contains:

<?php

namespace App\Core;


class SearchProcess
{
    public static function method_1(string $arg)
    {
        //Does things...
    }

    public static function method_2(string $arg)
    {
        //Does different things...
    }
}

The file is 'registered' in the config\app.php in the section aliases:

    'aliases' => [
        //Custom aliases...
        'SearchProcess' => App\Core\SearchProcess::class,
    ],

Finally I import it as:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

// use SearchProcess;
use App\Core;

class SearchController extends Controller
{

    public function search(Request $request)
    {
        //Does before staf...
        $search_value = SearchProcess::method_1($search_value_raw);

        //Does after staf...
    }

}

I run composer dump-autoload afterwards, but it does not change anything. I get an error:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'App\Http\Controllers\SearchProcess' not found

Moreover I do not know why it looks for the file in the controllers directory?

Upvotes: 0

Views: 1973

Answers (1)

// use SearchProcess;
use App\Core\SearchProcess;

Upvotes: 1

Related Questions