1)コントローラーを作成
php artisan make:controller Hello
app/Http/Controllers/Hello.phpが作成され
2)routes/web.phpに下記を追加し、public/helloにアクセスしてみると「Hello World!」が表示される
Route::get('/hello',function(){
return 'Hello World!';
});
3)routes/web.phpを変更してみる
Route::get('hello', 'Hello@index');
4)app/Http/Controllers/Hello.phpに下記を追加、再度public/helloにアクセスしてみる
public function index()
{
return 'hello world from controller : )';
}
5)Viewを追加してみる
resources/views/hello.blade.phpを作成、以下を追加
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
<link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
</div>
</div>
</body>
</html>
6)routes/web.phpに下記を追加
Route::get('/hello/{name}', 'Hello@show');
7)app/Http/Controllers/Hello.phpにshowファンクションを追加
public function show($name)
{
return view('hello',array('name' => $name));
}
8)public/hello/{name}にアクセスしてみる
参考:https://tutorials.kode-blog.com/laravel-hello-world
0 件のコメント:
コメントを投稿