Tuesday, December 26, 2017

Intoduction to laravel laravel packages

Step 1: First add your package to composer.jason autoload.

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"    ],
    "psr-4": {
        "App\\": "app/"        "Prabhath\\NewsManage\\": "app/Packages/prabhath/news-management/src/",
},
 
Register new package in Config/app.php 
 
NewsManage\NewsServiceProvider::class, 

Or in laravel 5.5 you can automatically register package when loading it (Composer update)
refer laravel documentation. https://laravel.com/docs/5.5/packages
 
 
 Step 2: Create folder structure
 
Create Packages folder in App folder.Then create packages category folder (prabhath).
After that create src and view folder with in category folder. Then Create Http and 
Models folders with in src folder. Then create Controllers and Request folder in 
Http folder.
 
  
 

Step 3: Create composer.jason
 
{
    "name": "prabhath/news-management",
    "description": "test package",
    "license": "MIT",
    "authors": [
        {
            "name": "prabhath wijesinghe",
            "email": "wdprabhath@gmail.com"        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-4": {
            "Prabhath\\NewsManagement\\": "src/"        }
    },
    "require": {
        "illuminate/support" : "~5"    }
} 

Step 4: Create route.php
 

Route::group(['middleware' => ['auth']], function(){    Route::group(['prefix' => 'news', 'namespace' => 'NewsManage\Http\Controllers'], function(){        Route::get('add', ['as' => 'news.add', 'uses' => 'NewsController@addView']);        Route::get('list', ['as' => 'news.list', 'uses' => 'NewsController@listView']);        Route::get('json/list', ['as' => 'news.list', 'uses' => 'NewsController@jsonList']);        Route::get('edit/{id}', ['as' => 'news.edit', 'uses' => 'NewsController@editView']);        Route::post('add', ['as' => 'news.add', 'uses' => 'NewsController@add']);        Route::post('delete', ['as' => 'news.delete', 'uses' => 'NewsController@delete']);        Route::post('edit/{id}', ['as' => 'news.edit', 'uses' => 'NewsController@edit']);
    });}); 
 
Step 5: create Model
 
Create your model in src/Models folder. News.php
<?php
namespace NewsManage\Models;
use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;
class News extends Model{
    use SoftDeletes;
    protected $table = 'tbl_news_line';
    protected $fillable = [        'user_id',        'news',        'created_at',        'updated_at',        'deleted_at'    ];    protected $guarded = ['id'];
    protected $dates = ['deleted_at'];

}
 
Step 6: Create Controllers NewsController.php 
 
 Same as the News model create NewsController.php in controllers 
folder. 
 
namespace NewsManage\Http\Controllers;
use App\Http\Controllers\Controller;
class NewsController extends Controller{    public function __construct()    {
    }    public function addView()    {        return 'add news view';    }
} 
 
Step 7: Create requests
 
 You can create requests for laravel validation in Request folder in 
http folder.

namespace NewsManage\Http\Requests;
use App\Http\Requests\Request;
class NewsRequest extends  Request{
    public function authorize()    {        return true;    }
    public function rules()    {        $rules = [            'news'=>'required|Max:110'        ];        return $rules;    }
}
 
Step 8: Create views 

Create views in Views folder.
 
Step 9: Create service provider in http folder and register
       views and models. (NewsServiceProvider.php)
 
namespace NewsManage;
use Illuminate\Support\ServiceProvider;
class NewsServiceProvider extends ServiceProvider{
    /**     * Bootstrap the application services.     *     * @return void     */ 
 public function boot()    {         
$this->loadViewsFrom(__DIR__.'/../views', 'newsManage');         
require __DIR__ . '/Http/routes.php'; 
 }
    /**     * Register the application services.     *     * @return void     */ 
 public function register() 
{         
$this->app->bind('newsManage', function($app)
{            return new NewsManage; 
});    }}
 
Step 10: Composer update 
 
sample package 
 
 
 
 
 
 




Share/Bookmark

0 comments:

Post a Comment