Laravel 5 Custom Package Class Not Found
I am creating laravel 5.2 package, following are my files
packages/
-Shreeji/
--Ring/
---composer.json
---src/
----Ring.php
----RingModel.php
----RingServiceProvider
composer.json
{
"name": "shreeji/ring",
"description": "Simple",
"license": "MIT",
"authors": [
{
"name": "author",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Shreeji\\Ring\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"Illuminate/support": "~5"
}
}
Ring.php
namespace Shreeji\Ring;
use Illuminate\Http\Response;
Class Ring {
private $ringmodel;
protected $table_name = null;
function __construct() {
}
function set_table($table_name)
{
$this->table_name = $table_name;
$this->ringmodel = New RingModel($this->table_name);
return $this;
}
}
RingModel.php
use \Illuminate\Database\Eloquent\Model as Eloquent;
class RingModel extends Eloquent {
// Set table name;
protected $table;
protected $primary_key;
public function __construct($table)
{
$this->table = $table;
}
}
RingServiceProvider.php
namespace Shreeji\Ring;
use Illuminate\Support\ServiceProvider;
Class RingServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('ring', function($app){
return new Ring;
});
}
public function boot()
{
}
}
And in app/Http/Controllers I have created test file like this
RingController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Shreeji\Ring;
class RingController extends Controller
{
public function index()
{
$ring = New Ring();
$ring->set_table('ring');
}
}
In Routes.php
Route::get('ringtest', [ 'as' => 'ringtest', 'uses' => '[email protected]' ]);
I have added service provider in config/app.php as
Shreeji\Ring\RingServiceProvider::class,
In composer.json I have added this as
.....
"psr-4": {
"App\\": "app/",
"Shreeji\\Ring\\": "packages/Shreeji/Ring/src"
}
.....
When I call ringtest from browser I get following error.
FatalErrorException in RingController.php line 19: Class 'Shreeji\Ring' not found
What is wrong with my code why this class is not found I have also run composer dumpautoload.
Answer
In your controller you have:
use Shreeji\Ring;
But, it must be:
use Shreeji\Ring\Ring;
The first 'Ring' is directory (namespace). The second 'Ring' is the class.
Your model is not in your namespace. The first line of your model must be:
namespace Shreeji\Ring;
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?