1.Giới thiệu
Artisan là giao diện dòng lệnh đi kèm với Laravel. Nó cung cấp một số lệnh hữu ích hỗ trợ bạn xây dựng ứng dụng của mình.
Để xem danh sách tất cả các lệnh Artisan có sẵn, bạn có thể sử dụng lệnh:
1 2 | php artisan list |
Chúng ta có thể tạo các models và controllers bằng cách sử dụng các lệnh Artisan dưới đây:
1 2 3 4 5 | php artisan make:model modelName php artisan make:controller controllerName |
Nhưng để tạo 1 View & Blade Templates bằng Artisan như dưới thì làm như thế nào ?
1 2 3 | php artisan make:view |
Research thoáng qua thì cũng có package Artisan View
để xứ lý việc này. Nhưng chúng ta muốn tạo 1 Artisan Console như thế thì làm như thế nào ?
Bài viết này sẽ hướng dẫn các bạn cách tạo Blade Template thông qua Artisan Console. php artisan make:view
2.Bắt đầu
- Tạo Commands: MakeView
php artisan make:command MakeView
- Code logic tạo view1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374namespace AppConsoleCommands;use IlluminateConsoleCommand;use File;class MakeView extends Command{/*** The name and signature of the console command.** @var string*/protected $signature = 'make:view {view}';/*** The console command description.** @var string*/protected $description = 'Create a new blade template.';/*** Execute the console command.** @return mixed*/public function handle(){$view = $this->argument('view');$path = $this->viewPath($view);$this->createDir($path);if (File::exists($path)) {$this->error("View {$path} already exists!");return;}File::put($path, $path);$this->info("View {$path} created.");}/*** Get the view full path.** @param $view* @return string*/private function viewPath($view){$view = str_replace('.', '/', $view) . '.blade.php';return "resources/views/{$view}";}/*** Create view directory if not exists.** @param $path*/private function createDir($path){$dir = dirname($path);if (!file_exists($dir)) {mkdir($dir, 0777, true);}}}
- Registered in App/Console/Kernel1234protected $commands = [MakeView::class];
3.Test
- Create a view home.blade.php trong thư mục mặc định
php artisan make:view home
- Create a view ‘home.blade.php’ trong sub-view (‘pages’)
php artisan make:view pages.home