이과/laravel 자료
[LARAVEL] controller - Route:get 이용해서 페이지 불러오기
코딩초밥
2022. 8. 9. 18:13
반응형
라라벨 처음 생성하고
web.php 에 들어가 보면
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
이렇게 처음에 세팅이 되어있다.
말그대로 url 에 아무것도 안붙어있으면 return 하여서 welcome 뷰를 보여줘라 라고 나와있는것이다.
아래에 보이는 기본 라라벨 페이지가 welcome view 이다.
그럼 반대로 이야기하자면
저 페이지로 네비게이션을 잡으면 될것이다.
url 파라미터값으로 확인을 하여서 view 랑 이어주면 된다.
하지만 저렇게 web.php 에 전체 페이지를 다 넣으면 기능 만들때
더욱 힘들다.
고로 controller 가 중간에서 그역활을 해줘야하는데
같이 한번 순서를 보자.
우선 controller 를 만들어주자.
php artisan make:controller PostController
이러면 controller 가 하나 만들어진다.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
}
기본적인 controller는 이렇게 생겼다.
이곳에 index 함수를 넣어서 return 기능을 만들어보자.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return 'This is index() from Postcontroller';
}
}
약간의 글을 써서 return 값을주었다.
이후 나는 /homepage 로 들어오는 url 은 'This is index() from Postcontroller'; 이라는 텍스트 반환값을 보여줄것이다.
(view를 보여주고싶다면 view로 만들면 그만이다.)
web.php 로 돌아와서 아까 만들어준 controller 안에있는 index 함수에 이어 준다.
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/homepage',[PostController::class, 'index']);
web.php 에서 postcontroller.php 를 거쳐서 return 값을 보여주는걸 알수있다.
반응형