jeudi 29 octobre 2015

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 :

 http://localhost/CSWeb/api/search/Slideshare

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

apiController.php

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

<?php

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

if i change code to

$controller=  new Slideshare; it works fine

Thank you in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire