0

I am developing a Laravel package that will use a custom artisan command i.e php artisan make:baserepository User -c. -c means I will create a controller as well.

I want when I run this artisan command, I am able to create a controller. Here is my code that creates the controller.

protected function createController() {
     $modelsingular = Str::singular(Str::ucfirst($this->getNameInput()));
     $modelplural = Str::plural($modelsingular);

     $controller = Str::studly(class_basename($this->argument('name')));
     $modelName = $this->qualifyClass($modelplural . '/' . $modelsingular);

     $this->call('make:controller', [
            'name' => "{$modelplural}\{$controller}Controller",
            '--model' => $this->option('resource') ? $modelName : null,
     ]);
}

Take a look at this line 'name' => "{$modelplural}\{$controller}Controller". I want the controller to be like this Admin\Admin.php in Http\Controllers, instead I am getting Admin\{Admin}Controller.php. Where am I getting it wrong?

I hope my question is clear.

1
  • 2
    Try concatenating the string with the point 'name' => $modelplural.'\'.$controller.'Controller'. Commented Nov 28, 2019 at 3:20

3 Answers 3

2

You can call your command with Artisan::call() function

As an example:

Route::get('/foo', function () {
    $exitCode = Artisan::call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
});

The original doc can be found here: Artisan #calling-commands-via-code

Sign up to request clarification or add additional context in comments.

Comments

1

@porloscerros comment helped me solve my problem, Here is how I did it

protected function createController() {
     $modelsingular = Str::singular(Str::ucfirst($this->getNameInput()));
     $modelplural = Str::plural($modelsingular);

     $controller = Str::studly(class_basename($this->argument('name')));
     $modelName = $this->qualifyClass($modelplural . '/' . $modelsingular);

     $this->call('make:controller', [
            'name' => $modelplural.'\'.$controller.'Controller',
            '--model' => $this->option('resource') ? $modelName : null,
     ]);
}

Comments

1
$a = 'there';

echo "hi\{$a}";
// hi\{there}

The \ before the { is the issue here.

echo "hi\\{$a}";
// hi\there

Now we are escaping the \ as \\.

"{$modelplural}\\{$controller}Controller"
// Admin\AdminController

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.