简体   繁体   中英

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

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:

  1. Rename the PHP file.
  2. Edit the PHP file to give the class the new controller name (the class name and the file name must match)
  3. Change any code that uses that old class name to use the new class name
  4. Edit the autoload files to load the new controller file:
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.

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.

忘记 CLI为什么不尝试使用“ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM