5

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.

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
Abaij
  • 853
  • 2
  • 18
  • 34
  • 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(); – shock_gone_wild Jul 10 '17 at 06:06

4 Answers4

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();
Peter Featherstone
  • 7,835
  • 4
  • 32
  • 64
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • Yes, function getService() is now accessible from controller. But I wonder why it return a string `"SELECT * FROM products"` instead of array of data? – Abaij Jul 10 '17 at 06:21
  • Got it! Use `DB::table('services')->get();` instead – Abaij Jul 10 '17 at 06:40
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);

}
Manish Arora
  • 259
  • 1
  • 4
  • 11
1

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

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Dinanath Thakur
  • 686
  • 4
  • 9
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.

Codeparl
  • 300
  • 1
  • 8