yii 权限和restful服务(一)增加自定义restful服务

本文详细介绍了如何配置URL管理器实现优雅的URL路由,通过AuthController展示具体操作及测试函数实现,以及通过访问特定URL获取用户信息的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<response>
<id>5</id>
<username>test12</username>
<auth_key/>
<password_hash/>
<password_reset_token/>
<email/>
<status>10</status>
<created_at>0</created_at>
<updated_at>1442992195</updated_at>
</response>

 1.在配置文件中

 

 'urlManager' => [
	    'enablePrettyUrl' => true,
	    'enableStrictParsing' => true,
	    'showScriptName' => false,
	    'rules' => [
	// 	    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
		    ['class' => 'yii\rest\UrlRule', 'controller' =>['auth','user'],'extraPatterns' => [
		    'GET {id}/testfun' => 'testfun', // 'xxxxx' refers to 'actionXxxxx' 'GET,HEAD {id}' => 'view',
		    ],],
		]
	    ],

其中 

'extraPatterns' => [
    'GET {id}/testfun' => 'testfun', // 'xxxxx' refers to 'actionXxxxx' 'GET,HEAD {id}' => 'view',
    ]

参考 UrlRules中的写法

 public $patterns = [
        'PUT,PATCH {id}' => 'update',
        'DELETE {id}' => 'delete',
        'GET,HEAD {id}' => 'view',
        'POST' => 'create',
        'GET,HEAD' => 'index',
        '{id}' => 'options',
        '' => 'options',
    ];

2.在AuthController中

 public function actionTestfun($id)
    {
    	$model = User::findOne(5);
   		return	$model;//var_dump('dsf');
    	// prepare and return a data provider for the "index" action
    }
    
3.访问http://www.niuhun.com/basic/web/auths/4/testfun

显示

<response>
<id> 5 </id>
<username> test12 </username>
<auth_key/>
<password_hash/>
<password_reset_token/>
<email/>
<status> 10 </status>
<created_at> 0 </created_at>
<updated_at> 1442992195 </updated_at>
</response>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值