InfyOmLabs / laravel-generator

API and Admin Panel CRUD Generator for Laravel.
https://www.infyom.com/open-source
MIT License
3.79k stars 812 forks source link

Prefixes config model added in prefix folder #884

Closed thewebartisan7 closed 4 years ago

thewebartisan7 commented 4 years ago

When I add prefixes like so:

    'prefixes' => [
        'route' => 'admin',  // using admin will create route('admin.?.index') type routes
        'path' => 'admin',
        'view' => 'admin',  // using backend will create return view('backend.?.index') type the backend views directory
        'public' => '',
    ],

Then also models are prefixes with Admin namespace, example:

namespace App\Models\Admin;

I would not expect this behaviour but to leave models without Admin prefixes, because the same model can be used in admin and frontend. On other hand controllers, routes and views are only used by admin so the prefixes should be only for this.

thewebartisan7 commented 4 years ago

I see now that config 'path' actually put controllers, request and models under Admin folder. That is fine for controllers and request, but models I think should not be. Or almost add separated path for models in case someone want to use it.

thewebartisan7 commented 4 years ago

A workaround for this is to setup custom path and namespace for controllers, requests and views. Example


<?php

return [
    'path' => [
        'request'           => app_path('Http/Requests/Admin/'), // CUSTOM path Admin
        'controller'        => app_path('Http/Controllers/Admin/'),  // CUSTOM path Admin
        'views'             => resource_path('views/admin/'),  // CUSTOM path Admin
    ],

    'namespace' => [
        'controller'        => 'App\Http\Controllers\Admin', // CUSTOM namespace Admin
        'request'           => 'App\Http\Requests\Admin', // CUSTOM namespace Admin
    ],

    'prefixes' => [
        'route' => 'admin',  // using admin will create route('admin.?.index') type routes
        'path' => '', // REMOVED admin
        'view' => '',  // REMOVED admin
        'public' => '',
    ],
];

But ideally would be better to add custom path for models in prefixes

thewebartisan7 commented 4 years ago

No, previous solution has an issue, the routes are created without namespace Admin:

Route::group(['prefix' => 'admin'], function () {
    Route::resource('articles', 'ArticleController', ["as" => 'admin']);
});
thewebartisan7 commented 4 years ago

Digging into the code I see that there is an undocumented configuration ignore_model_prefix see https://github.com/InfyOmLabs/laravel-generator/blob/develop/src/Common/GeneratorConfig.php#L139-L141

        if (config('infyom.laravel_generator.ignore_model_prefix', false)) {
            $this->nsModel = config('infyom.laravel_generator.namespace.model', 'App\Models');
        }

That allow to skip prefix for model.

I didn't yet tested but seem do exactly what I need.

In case this feature works I will update here. And then maybe you could add in docs.