Skip to content

Commit

Permalink
feat(controller): Allow using Builder controllers with custom Models/…
Browse files Browse the repository at this point in the history
…Controllers' directories
  • Loading branch information
anibalealvarezs committed Apr 4, 2022
1 parent 8e7953b commit a8c958a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Providers/PbControllerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function boot(Kernel $kernel)
if (Schema::hasTable('modules')) {
$models = PbModule::whereIn('modulekey', app(PbUtilities::class)->modulekeys)->pluck('modulekey');
foreach ($models as $model) {
$this->app->make($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix);
if (class_exists($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix)) {
$this->app->make($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix);
} elseif (class_exists('App\\Http\\Controllers\\'.ucfirst($model).$this->suffix)) {
$this->app->make('App\\Http\\Controllers\\'.ucfirst($model).$this->suffix);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/PbControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ public function buildControllerVars($key): object
$object->names = Str::plural($object->name);
$object->prefixName = strtolower($this->vars->helper->prefix . $key);
$object->prefixNames = Str::plural($object->prefixName);
$object->modelPath = $this->vars->helper->vendor . "\\" . $this->vars->helper->package . "\\Models\\" . $this->vars->helper->prefix . $key;
$object->viewsPath = $this->vars->helper->package . "/" . $object->keys . "/";
$object->modelPath = $this->vars->modelPath ?? $this->vars->helper->vendor . "\\" . $this->vars->helper->package . "\\Models\\" . $this->vars->helper->prefix . $key;
$object->viewsPath = ($this->vars->viewsPath ?? $this->vars->helper->package . "/" . $object->keys) . "/";
$object->table = resolve($object->modelPath)->getTable();
return $object;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Utilities/PbUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public function buildCrudRoutes($type): void
$names = PbModule::whereIn('modulekey', $this->modulekeys)->pluck('modulekey');

foreach ($names as $name) {
$modelClass = $this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name);
$controllerClass = $this->vendor . '\\' . $this->package . '\\Controllers\\' . ucfirst($name) . '\\' . $this->prefix . ucfirst($name) . 'Controller';
if (class_exists($this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name))) {
$modelClass = $this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name);
$controllerClass = $this->vendor . '\\' . $this->package . '\\Controllers\\' . ucfirst($name) . '\\' . $this->prefix . ucfirst($name) . 'Controller';
} elseif (class_exists('App\\Models\\' . ucfirst($name))) {
$modelClass = 'App\\Models\\' . ucfirst($name);
$controllerClass = 'App\\Http\\Controllers\\' . ucfirst($name) . 'Controller';
}
switch ($type) {
case 'web':
Route::resource($name . 's', $controllerClass)->middleware([
Expand Down

0 comments on commit a8c958a

Please sign in to comment.