close
<?php
/*檔案:app/http/controllers/controller.php*/
namespace App\Http\Controllers;
/*keyword same function name in many*/
/*specific class namesapce import this class*/
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
cmd輸入php artisan make:controller "你要取的名字"
<?php
/*檔案:app/http/controllers/你取的名字.php*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController2 extends Controller
{
//
}
cmd輸入php artisan make:controller --resource "你要取的名字"
<?php
/*檔案:app/http/controllers/你取的名字.php*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController3 extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
/*檔案:routes/web.php*/
Route::get('posts','PostController@index');
<?php
/*檔案:app/http/controllers/PostController.php*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return '成功啦';
}
}
瀏覽器網址列輸入:localhost/posts
出現文字:成功啦
/*檔案:routes/web.php*/
Route::get('posts/{id}','PostController@index');
<?php
/*檔案:app/http/controllers/PostController.php*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index($id)
{
return '這篇文章編號是'.$id;
}
}
瀏覽器網址列輸入:localhost/posts/2
出現文字:這篇文章編號是2
/*檔案:routes/web.php*/
Route::resource('posts','PostController');
<?php
/*檔案:app/http/controllers/PostController.php*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController3 extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
}
}
cmd鍵入php artisan route:list
將會出現(名字都自動命名好了)
| | GET|HEAD | posts | posts.index | App\Http\Controllers\PostController@index | web |
| | POST | posts | posts.store | App\Http\Controllers\PostController@store | web |
| | GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostController@create | web |
| | GET|HEAD | posts/{post} | posts.show | App\Http\Controllers\PostController@show | web |
| | PUT|PATCH | posts/{post} | posts.update | App\Http\Controllers\PostController@update | web |
| | DELETE | posts/{post} | posts.destroy | App\Http\Controllers\PostController@destroy | web |
| | GET|HEAD | posts/{post}/edit | posts.edit | App\Http\Controllers\PostController@edit | web |
全站熱搜
留言列表