PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] Laravel 5 custom package class not found

 January 19, 2022     laravel, laravel-5, laravel-5.2, php     No comments   

Issue

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@gmail.com"
     }
 ],
 "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.


Solution

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;


Answered By - schellingerht
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing