PHP code example of kareem22t / structure-my-module

1. Go to this page and download the library: Download kareem22t/structure-my-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kareem22t / structure-my-module example snippets


// web.php
Route::prefix('users')->group(function () {
    // For regular MVC
    Route::resource('users', UserController::class);

    // For Auth MVC
    Route::post('register', [UserController::class, 'register'])->name('register');
    Route::post('login', [UserController::class, 'login'])->name('login');
    Route::middleware('auth')->group(function () {
        Route::get('profile', [UserController::class, 'show'])->name('profile');
        Route::put('profile', [UserController::class, 'update'])->name('profile.update');
        Route::post('logout', [UserController::class, 'logout'])->name('logout');
    });
});

// api.php
Route::prefix('v1')->group(function () {
    // For regular API
    Route::apiResource('users', UserController::class);

    // For Auth Sanctum
    Route::post('register', [UserController::class, 'register']);
    Route::post('login', [UserController::class, 'login']);
    Route::middleware('auth:sanctum')->group(function () {
        Route::get('profile', [UserController::class, 'show']);
        Route::put('profile', [UserController::class, 'update']);
        Route::post('logout', [UserController::class, 'logout']);
    });
});

// Example for RegisterUserRequest.php
public function rules(): array
{
    return [
        'name' => '|min:8|confirmed',
    ];
}

// Example for UpdateUserRequest.php
public function rules(): array
{
    return [
        'name' => 'sometimes|string|max:255',
        'email' => 'sometimes|string|email|max:255|unique:users,email,' . auth()->id(),
        'password' => 'sometimes|string|min:8|confirmed',
    ];
}

// app/Models/User.php
use Laravel\Sanctum\HasApiTokens; // For auth-sanctum type

class User extends Authenticatable
{
    use HasApiTokens; // For auth-sanctum type

    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];
}
bash
php artisan install:api
bash
php artisan migrate
bash
php artisan make:module User auth-mvc
bash
php artisan make:module User auth-sanctum