처음부터 차근차근
artisan 명령어와 라우팅, 미들웨어, namespace, use 본문
artisan 명령어 모음
php artisan -V
Laravel Framework 버전 출력
php artisan list or php artisan
artisan 명령어 모두 보기
php artisan make:controller 컨트롤러파일명
controller 파일을 만든다.
php artisan make:model 모델파일명
모델 파일을 만든다.
php artisan make:model 모델명 -c -m
모델 파일을 만듦과 동시에 controller 파일과 migration 파일을 만든다.
controller와 migration 파일명은 라라벨에서 추천하는 명명 방식으로 알아서 만들어줌.
php artisan route:list
지금까지 만든 모든 라우팅 출력
resource 라우팅
php artisan make:controller 컨트롤러명 --resource
controller에 CRUD(Create, Read, Update, Delete) 기능을 지원한다.
이렇게 하면 controller에 index(), create(), store(), show(), edit(), update(), destroy()를 추가된다.
php artisan tinker
playground 같은거. 명령어 치고 결과를 볼 수 있다. (실제로 실행되지는 않음)
라우팅 하기
// 가장 기본적인 형태의 라우팅
Route::get('/', function () {
return view('welcome');
});
// /test를 치면 test.php 파일 실행
Route::get('test', function () {
return view('test');
});
// /test/아이디 적으면 아래 문장 출력
Route::get('test/{id}', function ($id) {
return "아이디는 ".$id."입니다.";
});
// /test/아이디/name/이름 적으면 아래 문장 출력
Route::get('test/{id}/name/{name}', function ($id, $name) {
return "아이디는 ".$id.", 이름은 ".$name."입니다.";
});
// route의 name을 user이라 정함
Route::get('user/{id?}', function ($user_id = 1) {
return "사용자 아이디는 ".$user_id."입니다.";
})->name('user');
// /hello를 치면 /user로 보내기
Route::get('hello', function () {
return redirect()->route('user');
});
// controller와 연동
// 함수가 없을 경우 -> 기본 함수(__invoke()함수)를 호출
use App\Http\Controllers\ExamController;
Route::get('con', ExamController::class);
// ExamController의 test_func()함수 호출
Route::get('confunc', [ExamController::class, 'test_func']);
Route::group(['namespace'=>'Admin'], function(){
});
// 공통된 url인 aaa에 대한 그룹핑 ex) locahost:8000/aaa/test, locahost:8000/aaa/second
Route::group(['prefix'=>'aaa'], function(){
Route::get('hi', function () { // locahost:8000/aaa/hi
});
});
+ 라우팅 헬퍼 사용하기
라우팅 name을 정해놓으면 (ex ->name('user'))view 파일 안에서 {{ route('user') }} 이렇게 사용할 수 있다.
url헬퍼를 사용하여 view 파일 안에다 url('/books')로 a 경로를 넣을 수 있다.
Middleware연결 방법
1. routes / web.php에서 하는 법
// Route::get('apple', function () {
// return "사과";
// })->middleware('auth');
// Route::get('banana', function () {
// return "바나나!";
// })->middleware('auth');
// ==
// 그룹지어서 미들웨어 사용하기
Route::group(['middleware'=>['auth']], function(){
Route::get('apple', function () {
return "사과";
});
Route::get('banana', function () {
return "바나나!";
});
});
2. Controller의 __construct()에서 설정
class ExamController extends Controller
{
public function __constructor() {
$this->middleware('auth');
}
}
namespace
해당 클래스가 위치하고 있는 폴더라고 생각하면 편하다.
use
모델 클래스 같은 외부 클래스에 접근하기 위해서는 그 모델 클래스가 있는 경로까지 다 써야한다.
예시 :
$books = App\Models\Book::all();
하지만 위에
use App\Models\Book;
를 써주면
$books = Book::all(); // books 테이블에 있는 모든 레코드를 가져와라
이렇게 짧게 써도 된다.
'Framework > Laravel' 카테고리의 다른 글
Laravel Model 관계(Eloquent Relationship) (0) | 2022.01.19 |
---|---|
form validation과 jetstream, 로그인한 user 알아내기, 리소스 라우팅 (0) | 2022.01.18 |
package 다운로드, 파일 명명 규칙, 리소스 컨트롤러, form 값 받기, db update/delete/create, fillable (0) | 2022.01.16 |
블레이드 템플릿과 데이터 넘기는 법, DB연결, Migration (0) | 2022.01.14 |
Laravel 설치와 VSCode 세팅, 라라벨 프로젝트 구조 (0) | 2022.01.13 |