如何生成嵌套资源的链接?

时间:2016-05-20 09:53:47

标签: php laravel controller routing resources

我正在处理的Laravel应用程序有两个资源。

第二种资源的路线如下:

    let onAnchorQueryResults : ((HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, NSError?) -> Void)! = {
        (query:HKAnchoredObjectQuery, addedObjects:[HKSample]?, deletedObjects:[HKDeletedObject]?, newAnchor:HKQueryAnchor?, nsError:NSError?) -> Void in

        if (addedObjects?.count > 0)
        {

            // var addDict = [String : AnyObject]()
            for obj in addedObjects! {
                let hkValue = obj as? HKQuantitySample
                if(hkValue?.UUID.UUIDString != nil){
                    print("TypeName:",type.isEqual("HKQuantityTypeIdentifierHeartRate"))
                    switch  type {
                    case  HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!:
                        let val =  (hkValue?.quantity.doubleValueForUnit(HKUnit(fromString: "count/min")))!
                        let uuid : String = (hkValue?.UUID.UUIDString)!

                    case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!:
                        let val =  (hkValue?.quantity.doubleValueForUnit(HKUnit.countUnit()))!
                        let uuid : String = (hkValue?.UUID.UUIDString)!


                    case HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!:
                        let val =  ((hkValue?.quantity.doubleValueForUnit(HKUnit.gramUnit()))!/1000.0)
                        let uuid : String = (hkValue?.UUID.UUIDString)!

                    default:
                        break

                    }

                }
            }

        }

    }

    let queryEndDate = NSDate()
    print("Before Start: ")
    print(self.queryStartDate)
    let predicate: NSPredicate = HKAnchoredObjectQuery.predicateForSamplesWithStartDate(self.queryStartDate, endDate: queryEndDate, options: HKQueryOptions.None)

    let anchoredQuery = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: hkAnchor, limit: 0, resultsHandler: onAnchorQueryResults)
    anchoredQuery.updateHandler = onAnchorQueryResults
    healthStore.executeQuery(anchoredQuery)
    self.queryStartDate = queryEndDate


}}

我将此设置称为嵌套资源,因为活动是在过程下定义的。定义或路线如下所示:

$ php artisan route:list | grep -i activity 

POST       | admin/procedure/{id}/activity                  | admin.procedure.{id}.activity.store   | (...)\ProcedureActivityController@store    
GET|HEAD   | admin/procedure/{id}/activity                  | admin.procedure.{id}.activity.index   | (...)\ProcedureActivityController@index    
GET|HEAD   | admin/procedure/{id}/activity/create           | admin.procedure.{id}.activity.create  | (...)\ProcedureActivityController@create   
GET|HEAD   | admin/procedure/{id}/activity/{activity}       | admin.procedure.{id}.activity.show    | (...)\ProcedureActivityController@show     
PUT|PATCH  | admin/procedure/{id}/activity/{activity}       | admin.procedure.{id}.activity.update  | (...)\ProcedureActivityController@update   
DELETE     | admin/procedure/{id}/activity/{activity}       | admin.procedure.{id}.activity.destroy | (...)\ProcedureActivityController@destroy  
GET|HEAD   | admin/procedure/{id}/activity/{activity}/edit  | admin.procedure.{id}.activity.edit    | (...)\ProcedureActivityController@edit     

我想为属于过程3的新活动生成一个POST操作的链接,就像我使用list-all-procedures路由一样;

Route::resource('procedure', 'ProcedureController');
Route::resource('procedure/{id}/activity', 'Admin\ProcedureActivityController');

有没有办法使用标准助手和外墙生成到嵌套资源的链接?

2 个答案:

答案 0 :(得分:1)

嵌套资源的路由定义不太正确。

var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();

date1>date2

应该是:

Route::resource('procedure/{id}/activity', 'Admin\ProcedureActivityController');

此外,我不确定您如何在URI中获取Route::resource('procedure.activity', 'Admin\ProcedureActivityController'); ,因为ResourceRegistrar将根据资源名称创建参数。根据您的第一个资源定义应该是{id}的定义。

您应该为索引路由找到类似{procedure}的路由名称。

admin.procedure.activity.index

Laravel 5.1 - Controllers - Restful - Nested Resources

  

route('admin.procedure.activity.index', ['procedure' => $id]);

     

此路线将注册“嵌套”资源,可通过以下网址访问:Route::resource('photos.comments', 'PhotoCommentController');

答案 1 :(得分:0)

您应该使用带有参数的route()来使其正常工作:

route('admin.procedure.{id}.activity.index', $id);