Skip to content

Instantly share code, notes, and snippets.

@tofuninjah
Created September 8, 2015 04:18
Show Gist options
  • Save tofuninjah/8e9affaf88e7320ab847 to your computer and use it in GitHub Desktop.
Save tofuninjah/8e9affaf88e7320ab847 to your computer and use it in GitHub Desktop.
custom class in Laravel 5
/** 3 seconds:
* - Library example class
* - Updated composer.json
* - Example usage in a controller
*/
/**
* '/library/Hello.php'
*/
<?php namespace Library;
class Hello {
static function world() {
return "Hello World!";
}
}
/**
* composer.json (just the autoload part, for brevity)
* Note "Library" has been added under "App"
* -> Run 'composer dump-autoload' in command line
*/
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Library\\": "app/library/"
}
},
/**
* '/app/Http/Controllers/TestController.php'
* Note that you'd need to route to this function...
*/
<?php namespace App\Http\Controllers;
use Library\Hello;
class TestController extends Controller {
public function index()
{
return Hello::world(); // should return "Hello World!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment