😀Hello Modules
C:\xampp82\htdocs\lva4\Modules\Blog\Config\config.php
<?php
return [
'name' => 'Blog'
];
C:\xampp82\htdocs\lva4\Modules\Blog\Database\Seeders\BlogDatabaseSeeder.php
<?php
namespace Modules\Blog\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class BlogDatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
}
}
C:\xampp82\htdocs\lva4\Modules\Blog\Http\Controllers\BlogController.php
<?php
namespace Modules\Blog\Http\Controllers;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index()
{
return view('blog::index');
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('blog::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('blog::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('blog::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
}
C:\xampp82\htdocs\lva4\Modules\Blog\Providers\BlogServiceProvider.php
<?php
namespace Modules\Blog\Providers;
use Illuminate\Support\ServiceProvider;
class BlogServiceProvider extends ServiceProvider
{
/**
* @var string $moduleName
*/
protected $moduleName = 'Blog';
/**
* @var string $moduleNameLower
*/
protected $moduleNameLower = 'blog';
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->register(RouteServiceProvider::class);
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig()
{
$this->publishes([
module_path($this->moduleName, 'Config/config.php') => config_path($this->moduleNameLower . '.php'),
], 'config');
$this->mergeConfigFrom(
module_path($this->moduleName, 'Config/config.php'),
$this->moduleNameLower
);
}
/**
* Register views.
*
* @return void
*/
public function registerViews()
{
$viewPath = resource_path('views/modules/' . $this->moduleNameLower);
$sourcePath = module_path($this->moduleName, 'Resources/views');
$this->publishes([
$sourcePath => $viewPath
], ['views', $this->moduleNameLower . '-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/' . $this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'));
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
}
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
$paths[] = $path . '/modules/' . $this->moduleNameLower;
}
}
return $paths;
}
}
C:\xampp82\htdocs\lva4\Modules\Blog\Providers\RouteServiceProvider.php
<?php
namespace Modules\Blog\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* The module namespace to assume when generating URLs to actions.
*
* @var string
*/
protected $moduleNamespace = 'Modules\Blog\Http\Controllers';
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*
* @return void
*/
public function boot()
{
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->moduleNamespace)
->group(module_path('Blog', '/Routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->moduleNamespace)
->group(module_path('Blog', '/Routes/api.php'));
}
}
C:\xampp82\htdocs\lva4\Modules\Blog\Resources\views\layouts\master.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Module Blog</title>
{{-- Laravel Vite - CSS File --}}
{{-- {{ module_vite('build-blog', 'Resources/assets/sass/app.scss') }} --}}
</head>
<body>
@yield('content')
{{-- Laravel Vite - JS File --}}
{{-- {{ module_vite('build-blog', 'Resources/assets/js/app.js') }} --}}
</body>
</html>
C:\xampp82\htdocs\lva4\Modules\Blog\Resources\views\index.blade.php
@extends('blog::layouts.master')
@section('content')
<h1>Hello World</h1>
<p>
This view is loaded from module: {!! config('blog.name') !!}
</p>
@endsection
C:\xampp82\htdocs\lva4\Modules\Blog\Routes\api.php
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/blog', function (Request $request) {
return $request->user();
});
C:\xampp82\htdocs\lva4\Modules\Blog\Routes\web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::prefix('blog')->group(function() {
Route::get('/', 'BlogController@index');
});
C:\xampp82\htdocs\lva4\Modules\Blog\Database\Migrations\2018_03_11_062135_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->string('name');
$table->string('slug')->nullable();
$table->text('intro')->nullable();
$table->text('content')->nullable();
$table->string('type')->nullable();
$table->integer('category_id')->unsigned()->nullable();
$table->string('category_name')->nullable();
$table->integer('is_featured')->nullable();
$table->string('featured_image')->nullable();
$table->string('meta_title')->nullable();
$table->text('meta_keywords')->nullable();
$table->text('meta_description')->nullable();
$table->string('meta_og_image')->nullable();
$table->string('meta_og_url')->nullable();
$table->integer('hits')->default(0)->unsigned();
$table->integer('order')->nullable();
$table->tinyInteger('status')->default(1);
$table->integer('moderated_by')->unsigned()->nullable();
$table->datetime('moderated_at')->nullable();
$table->integer('created_by')->unsigned()->nullable();
$table->string('created_by_name')->nullable();
$table->string('created_by_alias')->nullable();
$table->integer('updated_by')->unsigned()->nullable();
$table->integer('deleted_by')->unsigned()->nullable();
$table->timestamp('published_at')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
😎 Chu ý:
php artisan module:make-migration create_post_table Blog
C:\xampp82\htdocs\lva4\Modules\Blog\Database\Migrations\2023_10_23_035353_create_post_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('post');
}
};
php artisan module:migrate Blog
php artisan module:make-model Blog Blog
php artisan module:make-model Test -m Blog
C:\xampp82\htdocs\lva4\Modules\Blog\Database\Migrations\2023_10_23_035837_create_tests_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
};
C:\xampp82\htdocs\lva4\Modules\Blog\Entities\Blog.php
<?php
namespace Modules\Blog\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Blog extends Model
{
use HasFactory;
protected $fillable = [];
protected static function newFactory()
{
return \Modules\Blog\Database\factories\BlogFactory::new();
}
}
C:\xampp82\htdocs\lva4\Modules\Blog\Entities\Test.php
<?php
namespace Modules\Blog\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Test extends Model
{
use HasFactory;
protected $fillable = [];
protected static function newFactory()
{
return \Modules\Blog\Database\factories\TestFactory::new();
}
}
Last updated