如何解析Laravel路由文件?

时间:2017-04-23 11:56:47

标签: php regex laravel

roures.php 文件中包含

    Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController@create','middleware' => ['permission:role-create']]);
    Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController@store','middleware' => ['permission:role-create']]);
    Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController@show']);
    Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController@edit','middleware' => ['permission:role-edit']]);
    Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController@update','middleware' => ['permission:role-edit']]);
    Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController@destroy','middleware' => ['permission:role-delete']]);

    Route::get('itemCRUD2',['as'=>'itemCRUD2.index','uses'=>'ItemCRUD2Controller@index','middleware' => ['permission:item-list|item-create|item-edit|item-delete']]);
    Route::get('itemCRUD2/create',['as'=>'itemCRUD2.create','uses'=>'ItemCRUD2Controller@create','middleware' => ['permission:item-create']]);
    Route::post('itemCRUD2/create',['as'=>'itemCRUD2.store','uses'=>'ItemCRUD2Controller@store','middleware' => ['permission:item-create']]);
    Route::get('itemCRUD2/{id}',['as'=>'itemCRUD2.show','uses'=>'ItemCRUD2Controller@show']);
    Route::get('itemCRUD2/{id}/edit',['as'=>'itemCRUD2.edit','uses'=>'ItemCRUD2Controller@edit','middleware' => ['permission:item-edit']]);
    Route::patch('itemCRUD2/{id}',['as'=>'itemCRUD2.update','uses'=>'ItemCRUD2Controller@update','middleware' => ['permission:item-edit']]);
    Route::delete('itemCRUD2/{id}',['as'=>'itemCRUD2.destroy','uses'=>'ItemCRUD2Controller@destroy','middleware' => ['permission:item-delete']]);

预期输出:

[
  [
  `url` => 'roles/create',
  'as' => 'roles.create',
  'uses'=>'RoleController@create'
  ],
[
  `url` => 'roles/create',
   'as' => 'roles.store',
   'uses'=>'RoleController@store'
  ],
] 

如此

2 个答案:

答案 0 :(得分:3)

在这里,您可以使用file_get_content获取文件内容,我们使用正则表达式使用正则表达式解析文件内容。

正则表达式: /get\s*\(\s*'\K[^']+|as'\s*=>\s*'\K[^']+|uses'\s*=>\s*'\K[^']+/

  

get\s*\(\s*'\K[^']+此处符合示例: get('匹配到'

     

as'\s*=>\s*'\K[^']+此处符合示例: as'=>'匹配到'

     

uses'\s*=>\s*'\K[^']+此处符合示例: uses'=>'匹配到'

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string="Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController@create','middleware' => ['permission:role-create']]);
    Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController@store','middleware' => ['permission:role-create']]);
    Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController@show']);
    Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController@edit','middleware' => ['permission:role-edit']]);
    Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController@update','middleware' => ['permission:role-edit']]);
    Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController@destroy','middleware' => ['permission:role-delete']]);

    Route::get('itemCRUD2',['as'=>'itemCRUD2.index','uses'=>'ItemCRUD2Controller@index','middleware' => ['permission:item-list|item-create|item-edit|item-delete']]);
    Route::get('itemCRUD2/create',['as'=>'itemCRUD2.create','uses'=>'ItemCRUD2Controller@create','middleware' => ['permission:item-create']]);
    Route::post('itemCRUD2/create',['as'=>'itemCRUD2.store','uses'=>'ItemCRUD2Controller@store','middleware' => ['permission:item-create']]);
    Route::get('itemCRUD2/{id}',['as'=>'itemCRUD2.show','uses'=>'ItemCRUD2Controller@show']);
    Route::get('itemCRUD2/{id}/edit',['as'=>'itemCRUD2.edit','uses'=>'ItemCRUD2Controller@edit','middleware' => ['permission:item-edit']]);
    Route::patch('itemCRUD2/{id}',['as'=>'itemCRUD2.update','uses'=>'ItemCRUD2Controller@update','middleware' => ['permission:item-edit']]);
    Route::delete('itemCRUD2/{id}',['as'=>'itemCRUD2.destroy','uses'=>'ItemCRUD2Controller@destroy','middleware' => ['permission:item-delete']]);";


preg_match_all("/get\s*\(\s*'\K[^']+|as'\s*=>\s*'\K[^']+|uses'\s*=>\s*'\K[^']+/", $string,$matches);
$result=array();
for($x=0;$x<count($matches[0]);$x+=3)
{
    $result[]=array(
        'url'=>$matches[0][$x],
        'as'=>$matches[0][$x+1],
        'uses'=>$matches[0][$x+2]
    );
}
print_r($result);

答案 1 :(得分:2)

使用以下正则表达式匹配并替换:

匹配:.*\('([a-z\/{}0-9]+)',\['as'=>(['a-z\.0-9]+),'uses'=>(['a-zA-Z@]+).*

替换:[网址=> $1, 'as' => $2, 'uses' => $3]

PS。无论你来到哪个空间来匹配,都要使用\s *来对抗它