runningmark
runningmark

Reputation: 760

Unable to use helper in controller of laravel app

I'm building an application, now i'm created a helper

class Students{
    public static function return_student_names()
    {
        $_only_student_first_name = array('a','b','c');
        return $_only_student_first_name;
    }
}

now i'm unable to do something like this in controller

namespace App\Http\Controllers;
    class WelcomeController extends Controller
    {
        public function index()
        {
            return view('student/homepage');
        }
        public function StudentData($first_name = null)
        {
            /* ********** unable to perform this action *********/
            $students = Student::return_student_names();
            /* ********** unable to perform this action *********/
        }
    }

this is my helper service provider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;


class HelperServiceProvider extends ServiceProvider
{

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        foreach(glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
    }
}

i event added it as an alias in config/app.php file

'Student' => App\Helpers\Students::class,

Upvotes: 0

Views: 4122

Answers (2)

manix
manix

Reputation: 14747

You do not need a service provider to make it works. Just lets the Students class as you did:

class Students{
    public static function return_student_names()
    {
        $_only_student_first_name = array('a','b','c');
        return $_only_student_first_name;
    }
}

all its methods should be static

You added the Facade correctly:

'Student' => App\Helpers\Students::class,

Finally, looks like your problem is caused by forgetting a backslash at facade name. Uses \Students instead of Students:

public function StudentData($first_name = null)
{
    $students = \Student::return_student_names();
}

When using a facade, it is not necessary makes nay include, the facades were made to avoid complex includes in everywhere.

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24671

Try putting use App\Helpers\Student; at the top of your controller beneath the namespace delcaration:

namespace App\Http\Controllers;

use App\Helpers\Student;

class WelcomeController extends Controller
{
    // ...

Look more into PHP namespaces and how they are used, I believe you may have a deficient understanding about them. Their only purpose is to make so you can name and use two classes with the same name (e.g. App\Helpers\Student vs maybe App\Models\Student). If you needed to use both of those classes inside of the same source file, you can alias one of them like this:

use App\Helpers\Student;
use App\Models\Student as StudentModel;

// Will create an instance of App\Helpers\Student
$student = new Student(); 
// Will create an instance of App\Models\Student
$student2 = new StudentModel(); 

You do not need to have a service provider for this, just the normal language features. What you would need a service provider for is if you wanted to defer the construction of your Student object to the IoC:

public function register()
{
    $app->bind('App\Helpers\Student', function() {
        return new \App\Helpers\Student;
    });
}

// ...
$student = app()->make('App\Helpers\Student');

You should never have to include or require a class file in laravel because that is one of the functions that composer provides.

Upvotes: 1

Related Questions