天天看點

tp6注解路由安裝報錯了咋整?怎麼寫一個簡單的注解路由

作者:陳業貴 華為雲享專家 51cto(專家部落客 明日之星 TOP紅人)

文章目錄

  • ​​前言​​
  • ​​一、安裝注解路由(報錯是因為不指定版本)​​
  • ​​2.代碼​​
  • ​​效果​​
  • ​​tp6架構結構​​
  • ​​切記,使用注解路由時,index應用下的route檔案夾要重命名為其他的,不然不能生效​​

前言

關于注解路由的基本使用+安裝注解路由

一、安裝注解路由(報錯是因為不指定版本)

composer require topthink/think-annotation ^1.0      

2.代碼

<?php
declare (strict_types = 1);

namespace app\index\controller;//命名空間,以app開始檔案夾開始,controller結束
use think\annotation\Route;//注解路由的引用
class Index
{
    /**
     * @param  string $name 資料名稱
     * @return mixed
     * @Route("hello/:name")
     */
     //注解路由:hello方法的:name參數,也就是這裡的cyg
    public function hello($name)//套用tp6文檔的代碼
    {
        return 'hello,'.$name;
    }
}      

效果

tp6架構結構

切記,使用注解路由時,index應用下的route檔案夾要重命名為其他的,不然不能生效