Reputation: 841
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' => 'RingController@index' ]);
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.
Upvotes: 2
Views: 4397
Reputation: 1539
Your namespaces are correct except when your importing the Ring class to your Ring controller
In your Ring controller change
use Shreeji\Ring;
to
use Shreeji\Ring\Ring;
where the first Ring is the folder and the second Ring is the class.
Next on the first line of your file add a namespace for your RingModel class
namespace Shreeji\Ring;
and add
use Shreeji\Ring\RingModel;
to the Ring class to import the RingModel class.
Then in your package's composer file change
"Shreeji\\Ring\\": "src/"
to
"Shreeji\\": "packages/shreeji/ring/src"
After that navigate to your package's directory and run
composer dump-autoload -o
in your terminal or command prompt to regenerate composer.json file .
Next add
"Shreeji\\": "packages/shreeji/ring/src"
to your Laravel application's composer.json file inside the autoload psr-4
array and then switch to your Laravel's application folder and run
composer dump-autoload -o
in your terminal or command prompt to regenerate composer.json file.
Here is a link on Laravel package development.
Upvotes: 0
Reputation: 5806
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;
Upvotes: 6