หมวดหมู่: PHP

Laravel: Artisan clear cache, optimize, infoLaravel: Artisan clear cache, optimize, info

การเขียน laravel มันจะมีส่วนที่ต้องเข้าไปใน command เพื่อที่จะใช้ php artisan xxx บ่อยมากโดยเฉพาะที่เกี่ยวกับ cache ส่วนต่าง ๆ เลยทำทางลัด short cut จัดการจะได้ทำได้ง่าย ๆ

เปิดไฟล์ /routes/web.php แล้วเพิ่ม

//refresh cache
Route::get ('/refresh', function () { //php artisan optimize:clear $commands = [ //clear 'auth:clear-resets', 'optimize:clear', 'queue:clear', 'schedule:clear-cache', //add 'cache:table', 'lang:publish', 'optimize', 'stub:publish', //'ziggy:generate', //information 'about', //'db:show', 'env', 'list', //all commands 'package:discover', 'route:list', //may fail if there are errors in routes ]; $output = '<!DOCTYPE html><html lang="en"><head><title>refresh</title></head><body><dl>'; foreach ($commands as $command) { try { Artisan::call ($command) ; $results = Artisan::output () ; $results = explode ("\n", $results) ; $results = implode ("</dd><dd>", $results) ; $output .= '<dt>php artisan ' . $command . '</dt><dd>' . $results . '</dd>'; } catch (Exception $e) { $output .= '<dt>php artisan ' . $command . '</dt><dd><h2 style="color: red;">Error executing command</h2>: ' . $e->getMessage () . '</dd>'; } } $output .= '</dl><br>All caches have been cleared and recreated.</body></html>'; return response ($output) ->header ('charset', 'utf-8') ->header ('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->header ('Content-Type', 'text/html') ->header ('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT') ->header ('Pragma', 'no-cache') ->header ('X-Accel-Buffering', 'no') ;
}) ;

หลังจากนั้นอย่าลืม ใช้คำสั่ง
php artisan optimize
เพื่อ update config, events, routes, views ก่อน

การใช้ก็ง่าย ๆ แค่ ใส่ใน array แล้วเรียก URL ประมาณ http://127.0.0.1:8000/refresh ก็จะจัดงานที่ต้องใช้ php artisan ให้เราได้แล้ว CI/DI เล็ก ๆ

เพื่อความปลอดภัย บางคำสั่งอย่าง php artisan db:show ใน production ควรจะเอาออก / comment เอาไว้หรือตรวจสอบให้เฉพาะคนที่มีสิทธิ์เข้าใช้

จะปีใหม่แล้ว ล้างสิ่งเก่า ๆ เริ่มต้นส่ิงใหม่ ๆ กันครับ