---
title: laravel的網址顯示
tags: laravel
---
route
===
1. web.php
* 顯示頁面的url可以隨便設定需要讓使用者看到的網址名稱
* 顯示的頁面內容要對應views資料夾內的檔案
如果有資料夾的話用.做區分 如 file.index
```php=
Route::get('/顯示頁面的url', function () {
return view('顯示的頁面內容');
});
```
2. 使用controller
* 先在routes/web.php告訴他要使用的方法是哪一種
及對應到哪一個class(StudentController::class)
以及要用的是哪一個方法(index)
如下:我要用get的方式走USER的路徑去訪問userController的這支class
接著去訪問index的這個程式
```php=
Route::get('/student', [StudentController::class, 'index']);
```
* 在app/http/Controller建立StudentController.php
```php=
php artisan make:controller StudentController
```
* 上方use它的路徑
```php=
use App\Http\Controllers\StudentController;
```
* 在Controllers\StudentController.php裡面給他一個function
```php=
public function index(){
return view('student');
}
```
* 在views裡面創一個student.blade.php
*
3. 網址使用兩個變數
* 在web.php裡面使用
```php=
Route::get('/student1010/{name}/{num}',function ($name,$num){
$date = [
'name' => $name,
'num' => $num
];
return view('student1010',['data'=>$date]);
});
```
3. 轉址
從here來 要轉到there去
```php=
Route::redirect('/here','/there');
```
4. 如果只是要單純回傳 可直接用以下方法
```php=
Route::view('/','welcome');
```
group
===
* 把取得網址的那些function整合成一個function
```php=
Route::prefix('admin')->group(function () {
Route::get('/student',function () {
// Matches The "/admin/users" URL
// dd('admin student');
return view('student');
});
Route::get('/product',function () {
// Matches The "/admin/users" URL
// dd('admin product');
return view('product');
});
});
```
Resource Controller
===
1. 創造一個PhotoController.php的檔案
* 在Bash裡面輸入下面這行 (Photo是名字 須是單數)
```php=
php artisan make:controller PhotoController --resource
```
* 打完會出現

2. use它
* 在web.php裡面輸入
```php=
use App\Http\Controllers\PhotoController;
```
3. 賦予網址
* 在web.php下面輸入
```php=
Route::resource('photos', PhotoController::class);
```
4. 測試
* 網址輸入http://localhost:8000/bikes 確認是否完成
5. URI的參考

index=首頁
create=新增的頁面