laravel 5.1 – Dynamically create Class object based on string

i want to create object of class base on string which come from URL parameter.

for example :


in above URL Slideshare is parameter which get in apiController->indexAction.


namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Http\API\Slideshare;

class apiController extends Controller

    public function index($source)
        $controller=  new $source;
        return $controller->index();
        // if i change code to  $controller=  new Slideshare; it works fine


slideshare.php class


namespace App\Http\API;

class slideshare 

    public function index()
         return 'any data'; 



laravel error when i use parameter string to create class object

FatalErrorException in apiController.php line 17: Class
‘Slideshare’ not found


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s