Hi,
I have a table called Company and a table called Employee that has a foreign key (company_id). I Created the Model and Controller at GII and changed the controller like they said at the docs and it runned perfectly.
But i want just to do a little adjust. In my employee endpoint i want to pass the company_id in the URL. I tried this code:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
'PUT,PATCH <company_id:\d+>/employee/<id>' => 'employee/update',
'DELETE <company_id:\d+>/employee/<id>' => 'employee/delete',
'GET,HEAD <company_id:\d+>/employee/<id>' => 'employee/view',
'POST <company_id:\d+>/employee' => 'employee/create',
'GET,HEAD <company_id:\d+>/employee' => 'employee/index',
'<company_id:\d+>/employee/<id>' => 'employee/options',
'<company_id:\d+>/employee' => 'employee/options',
],
],
But if i access from /employee and /id/employee, both works…how can i do it correctly and change a once to every employee endpoint?
Is there any way that i get this company_id inside of the Model and filter directly? Without override the controller’s action?
Thank’s
1 Like
CeBe
May 23, 2014, 3:07pm
2
Not sure I understand your question but company_id will be avilable in $_GET.
Hello CeBe…thank’s for the answer…
it’s just a pattern that i use with REST…if i want to get all employees from a company, i use the endpoint:
http://localhost/companies/1/employees
Do u understand what i want to do?
CeBe
May 28, 2014, 12:22pm
4
okay, so have you tried with $_GET[‘company_id’] ?
baska911
(Popalexd)
May 28, 2014, 6:35pm
5
If you want to use this url http://localhost/companies/1/employees you can use this settings for url manager in your config file assuming you have a CompanieController
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'companie', 'extraPatterns' => [
"GET {company_id}/employees" => "employees"
],
'tokens' => [
'{company_id}' => '<company_id:\\d[\\d,]*>',
],
],
],
],
and now you can easily GET your company_id in your employees action using $_GET[‘company_id’].
evstevemd
(Stefano Mtangoo)
May 29, 2014, 9:26am
6
If you want to use this url http://localhost/companies/1/employees you can use this settings for url manager in your config file assuming you have a CompanieController
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'companie', 'extraPatterns' => [
"GET {company_id}/employees" => "employees"
],
'tokens' => [
'{company_id}' => '<company_id:\\d[\\d,]*>',
],
],
],
],
and now you can easily GET your company_id in your employees action using $_GET[‘company_id’].
Interesting…!
Can you explain how it works?
If you want to use this url http://localhost/companies/1/employees you can use this settings for url manager in your config file assuming you have a CompanieController
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'companie', 'extraPatterns' => [
"GET {company_id}/employees" => "employees"
],
'tokens' => [
'{company_id}' => '<company_id:\\d[\\d,]*>',
],
],
],
],
and now you can easily GET your company_id in your employees action using $_GET[‘company_id’].
alexkb9, thank’s for your anwser.
I know that is a little magic, but when u use REST in YII 2.0, the ActiveController take care of everything…so will i need to override every action or is there any way to use the $_GET[‘company_id’] inside the Model?