How can i rename laravel controller with command line interface(CLI)? - php

I created controller which name is Home but I want rename it with HomeController. Is it possible in laravel?
Example:
class Home extends Controller { }
I Want to rename it like
class HomeController extends Controller { }
I Want to do it with CLI.

You can't use artisan, but you can do it manually. But it's not just a simple rename.
You have to:
Rename the PHP file.
Edit the PHP file to give the class the new controller name (the class name and the file name must match)
Change any code that uses that old class name to use the new class name
Edit the autoload files to load the new controller file:
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php

You have to do it manually, there is no any command available to updated controller name.

Make a new Controller with the new Name.
php artisan make:controller NewController
or
php artisan make:controller NewController --resource
//if it is a resource controller
Now copy all contents from Old Controller to NewController.
Then edit all routes (in web.php) to that Controller.

forget CLI why not try rename the controller using 'AS'
use app\MyController as MyControl;

I think doing composer dump-autoload this regenerates the files :
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php

You can assess the file using nano command or vi, it depends on your machine. it basically an editor the enable you to change the content of the file.

Related

How to create controller inside folder app but outside folder http in laravel?

I have a folder Web inside folder app of Laravel. And i want to create Controller inside folder Web. But when i run commandline :
$ php artisan make:controller app/Web/Controllers/Webcontroller
It create controller inside folder Http/Controllers as default. So how to do that?
You could try this:
php artisan make:controller ../../App/Web/Controllers/WebController
or create the controller manually as Jerodev suggests in the comments of the question.
What laravel suggest is
It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.
If you choose to nest your controllers deeper into the App\Http\Controllers directory, use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you should register routes to the controller like so:
Route::get('foo', 'Photos\AdminController#method');
so if you create your controller out side Controllers directory you may have to do extra work for it to work.

Laravel5.6 - override a vendor view

I try to make my own version of a vendor blade template.
I dont want to extends the controller with the reference of the view.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(__DIR__.'/../../resources/views/oauth/passport', 'passport');
I created a file named authorize.blade.php in /resources/views/oauth/passport
In the vendor controller method we can see this:
return $this->response->view('passport::authorize');
The problem is when I call the vendor controller method it loads his version of authorize.blade.php. I would like mine to be loaded and I expected the new line I added to AppServiceProvider to do that.
Passport comes with VUE components and views you need to publish first to override them. From the Laravel Passport page:
"If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command."
All you need to do is run php artisan vendor:publish --tag=passport-views and the vendor views will be place in resources/views/vendor/passport, where you can edit them.
Use can use php artisan vendor:publish --tag=passport-views this will copy the views to your views folder for you to change.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(DIR.'/../../resources/views/oauth/passport',
'passport');
You can use this option only by placing it in registry () instead of boot(). And then you can use your Views regardless of whether they were published in Vendor or not

Laravel: How to generate seeders in a custom directory

I'm using Laravel 5.5 with laravel-modules v2.
It's easy to generate migrations in a custom directory (inside a module, specifically):
php artisan make:migration create_users_table --path=Modules/User/Database/Migrations
But seems that this isn't possible with seeding classes:
php artisan make:seeder UsersTableSeeder --path=Modules/User/Database/
The "--path" option does not exist.
or passing full relative path:
php artisan make:seeder Modules/User/Database/Migrations/UsersTableSeeder
Creates this exactly folder structure inside ./database/seeds/
or passing full absolute path:
php artisan make:seeder /Modules/User/Database/Migrations/UsersTableSeeder
file_put_contents(\my\file\system\project\database/seeds/C:/Program Files/Git
/Modules/User/Database/Seeders/UsersTableSeeder.php): failed to open str
eam: No such file or directory
How to generate seeders with artisan command in a custom directory?
You can't. The GeneratorCommand (which the Seeder extends) doesn't care about whether or not folders exist, because it's just going to write the file only.
/**
* Get the destination class path.
*
* #param string $name
* #return string
*/
protected function getPath($name)
{
return $this->laravel->databasePath().'/seeds/'.$name.'.php';
}
The only way to achieve what you want is to write your own Seeder command and allow for directory traversal. You can inspect the Illuminate\Database\Console\Migrations\MigrateMakeCommand to see how it's done, it's not very difficult.
You can't but you can do it with your custom code after do following steps:
Copy file from Illuminate/Database/Console/Seeds/SeederMakeCommand.php to your
folder.
Modify getPath() function.
Bind this class to container to override core.
I had same problems bcs in our team we want to create two folder, one for real data seeders and the other for fake data seeders.
in DatabaseSeeder file you can define your classes from another path ( namespace ) and it will work after running php artisan db:seed command
in my case I define new call :
$this->call([
//real data classes from \database\seeds path
]);
$this->call([
//fake data from database\seeds\tests path
'Database\Seeds\Tests\AuthControllerTestSeeder'
]);
do not forget to define your namespace (Database\Seeds\Tests\AuthControllerTestSeeder) in

Laravel: Edit file in the vendor directory

I am working with laravel and I have installed a package using composer by running this command composer require mailchimp/mailchimp=~2.0.
After that I got a folder 'mailchimp' in the vendor directory. In there, there is a file named Mailchimp.php that I have to modify, but based on some old posts here, if I modify the file, any time I run the command composer update, I will loose my changes in the file, just because it is located in the vendor directory. So is there any option for me to solve this problem ?
I tried using the command php artisan vendor:publish but I do not get the expected results.
You can create a custom class which will extend the Mailchimp class and override the function you want. Then use the custom class in your code.
use DrewM\MailChimp\MailChimp;
class CustomMailChimp extends MailChimp {
...
// The function you would like to override
}
Then use it new CustomMailChimp(..)

How do I instruct artisan to save model to specific directory?

I'm using Laravel 5. I have created a /Models directory under the /App directory, but when generating the models using Artisan it's storing them under the App directory.
I have searched the documentation to try and find how to specify a different path name, but to no avail:
php artisan make:model TestModel
How do I instruct artisan to save the model to specific directory?
Create a Models directory or whatever your want to named it, put it in inside app directory. The Directory structure should look like
laravel-project
/app
/Console
/Events
/Exceptions
/Http
/Jobs
/Listeners
/Provider
/Models
Then You just need to type artisan command for creating models inside Models directory
php artisan make:model Models/ModelName
After Creating Models your namespace inside model classes will be
namespace app-name\Models\ModelName
You can access this model in inside your controller
use app-name\Models\ModelName
In Laravel 5.4 or later
You can create as below
> php artisan make:model "Models\userModel"
here Models is directory name and userModel is model name
Use " (double quotes) or ' (single quotes) to create model
For those using Laravel >= 5.2
It is possible to generate a model in a subdirectory using built-in Artisan generators by "escaping" the backslashes in the FQN, like so:
Laravel 5.2
php artisan model:make App\\Models\\Foo
Laravel 5.3
php artisan make:model App\\Models\\Foo
(the difference between 5.2 and 5.3 pointed out by #Khaled Rahman, thanks!)
Commands above would create Foo.php file in the app/Models directory and update the namespace accordingly.
Hope that helps.
If you want to specify the path when generating a model, you can use the Laravel Generators Package. You can then specify the location using the --path option like so:
php artisan generate:model TestModel --path=my/custom/location
You can override the default Laravel command with this console class
<?php
namespace App\Console\Commands;
use Illuminate\Foundation\Console\ModelMakeCommand;
class ModelMake extends ModelMakeCommand
{
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\\Models';
}
}
The next time you make a model
php artisan make:model Example
your class will be placed in
App\Models\Example.php
Edit: Laravel 8 natively support this. I copied this snippet from somewhere, I just can't remember where to credit.
You can simply use this (for Laravel 5.3+):
php artisan make:model your_path/model_name
If you don't specify a path it will use main 'app' folder as root. So you can navigate from there.
Models/Folder1/MyModel means app->Models->Folder1->MyModel.php
This works for actual Laravel version, 5.6.28, on Windows 7
php artisan make:model App\Models\NewModel
Note: Do not use double escapes ('\\')
This generate the file App\Models\NewModel.php as follows
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class NewModel extends Model
{
//
}
In Laravel 8+ with default Models directory it can be achieved like bellow.
php artisan make:model App\Modules\ModuleName\Models\ModelName
It generates model file in directory
app/Modules/ModuleName/Models/ModelName.php
<?php
namespace App\Modules\ModuleName\Models;
use Illuminate\Database\Eloquent\Model;
class ModelName extends Model {
...
}
}
Add this line in composer.json file if it is not defined.
"autoload": {
"psr-4": {
"App\\": "app/",
//...
}
Then in your terminal do;
php artisan make:model Models/your_path/model_name
Controller path (ApI/Admin)
Model path(Model/Admin)
php artisan make:controller API/Admin/PlanController --model=Model/Admin/Plan --resource

Categories