6

I just read this post to make a global function which is able to be accessed from any controller. But I don't understand how it works.

I want to make variable 'services' accessible from any controller. So, I make General.php and put it in app/Http. Here is the code.

<?php
class General {

   public function getServices() {
      $services = "SELECT * FROM products";
      return $services;
   }
}

And in the controller I include it

<?php
namespace App\Http\Controllers;

use App\Http\General;
use Illuminate\Http\Request;

class HomeController extends Controller {
   public function index() {

       $title = 'Our services';
       $services = General::getServices();

       return view('welcome',  compact('title','services'));

   }
}

When I run it I got error Class 'App\Http\General' not found. And then how I can Anyone can help would be appreciated.

1
  • 2
    2 errors in your code: First: You have add the corrrect namespace at the top of your general class. Second: The getServices() method has to be declared static if you want to use it like General::getServices(); Commented Jul 10, 2017 at 6:06

4 Answers 4

12

First create the required function inside the app directory within a .php file as

helpers.php

if (!function_exists('getServices')) {
    public function getServices() {
        return DB::table('services')->get();
    }
}

and include this file in composer.json inside autoload/files array as

composer.json

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php"
    ]
},

Then update the composer, now you can able to directly use the created function inside your whole project as the file is automatically loaded when application get bootstraped

$result = getServices();
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, function getService() is now accessible from controller. But I wonder why it return a string "SELECT * FROM products" instead of array of data?
Got it! Use DB::table('services')->get(); instead
2

First You Create a General file in app directory named General.php

<?php 
namespace App;
use Illuminate\Http\Request;

class General {

public function __construct()
{

}

public function getServices() {
  $services = "SELECT * FROM users";

  return $services;
}
}

And then in Controller you want to call this call

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\General;

class HomeController extends Controller {

    $general = new General();
    $services = $general->getServices();
    dd($services);

}

Comments

1
+100

if you want to use any global function, then you can create your own helper class or function, then register that class in autoload, that is all you have to do. Now, wherever you want, you can call that function

Comments

0

I had a problem when I was trying to register my helpers.php file within the composer.json. I had always getting function not defined error due to the fact that I included a namespace at the top of the file, but when removed it everything worked like a charm for me. So what to follow is the following:

  1. create your helpers.php file and place it in the app or bootstrap directory.

  2. make sure not to include namespace if you just want to use your functions.

  3. include in your composer.json app/helpers.php or bootstrap/helpers.php, depending where you put the file.

  4. run the command composer dump-autoload

  5. Now your functions will be accessed globally everywhere in your app.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.