More readable syntax for routes.php :)

Submitted by imanghafoori - 9 years ago

These are helper functions which allow you to write more concise router code:

/**
note:
1- It does not override or replace the default syntax. (you can use the default too)
2- for url-prefix in group() the "/..." at the end is optional.
3- namespace in group() should be after the @ or $ or & 
4- middlewares should be after a | at the end.
5- route names should be in ( ).
6- spaces are simply ignored.

*DO NOT forget to hit like or comment if you liked it.*
*/

//             "/..." is optional
group('some-url/...  @Acme | auth', function() 
{
    get( '/url' , 'HomeController@index (routeName) | auth,logger ');  // brackets are optional    
    resource('this-url/' , 'HomeController++(index, show)' ); //only  index and show 
}



// in other words these work the same :
Route:group(['prefix'=>'some-url' , 'namespace'=>'Acme' , 'middleware'=>'auth'] , function(){});
group('some-url/...  @Acme | auth', function(){});

//------------------------------------------------------------------------

get( '/url' , ['as'=>'routeName','uses'=>'HomeController@index','middleware'=>['auth','logger']]); 
get( '/url' , 'HomeController@index (routeName) |   auth , logger ');

//------------------------------------------------------------------------

resource('this-url/' , 'HomeController' , ['only' => ['index' , 'show']] );
resource('this-url/' , 'HomeController++(index, show)' );

//------------------------------------------------------------------------

resource('that-url/' , 'HomeController' , ['except' =>['post' , 'delete']] );
resource('that-url/' , 'HomeController--(post, delete)');

//-------------------------------------------------------------------------

get( '/url'   , ' HomeController@index ( routeName )  | auth , logger ');  // is equal:
get( '/url'   , 'HomeController@index(routeName)|auth,logger ');  // to this

//------------------------------------------------------------------------

resource('this-url/' , 'HomeController++(index, show)' );
resource('this-url/' , 'HomeController+(index, show)' );

//------------------------------------------------------------------------

resource('that-url/' , 'HomeController--(post, delete)');
resource('that-url/' , 'HomeController-(post, delete)');

//------------------------------------------------------------------------

get( '/url' , 'HomeController@index (routeName) |  auth , logger ');
get( '/url' , 'HomeController@index (routeName) |["auth","logger"] ');

//------------------------------------------------------------------------

group('some-url/...  @Acme | auth', function(){});
group('some-url      @Acme | auth', function(){}); // The /... is optional

//------------------------------------------------------------------------






// how to use it  ???
// in your bootstrap/autoload.php file :

require __DIR__.'/../my-helpers.php';
require __DIR__.'/../vendor/autoload.php';


///* put this code in your my-helper.php *///
        function resource( $name , $controller , array $options = [ ] )
	{
		$controller = preg_replace( '/\s*?/' , '' , $controller );                  // stripes spaces

		if( preg_match( '/.*?\+\+?[\(\[](.*)[\]\)]/' , $controller ) )            // if it was like homeControlle++(get,post)
		{
			$option            = preg_replace( '/.*\+\+?[\(\[](.*)[\]\)]/' , '$1' , $controller ); // take out the verbs
			$options[ 'only' ] = preg_split( '/\s*,\s*/' , $option );             // split verbs by commas
			$controller        = preg_replace( '/(.*?)\+\+?.*/' , '$1' , $controller ); // take out the controller name
		}


		if( preg_match( '/.*?--?[\(\[](.*)[\]\)]/' , $controller ) )            // if it was like homeControlle--(get,post)
		{
			$option            = preg_replace( '/.*--?[\(\[](.*)[\]\)]/' , '$1' , $controller ); // take out the verbs
			$options[ 'only' ] = preg_split( '/\s*,\s*/' , $option );             // split verbs by commas
			$controller        = preg_replace( '/(.*?)--?.*/' , '$1' , $controller ); // take out the controller name
		}

		$controller = preg_replace( '/.*\+\((.*)\)/' , '$1' , $controller );

		return app( 'router' )->resource( $name , $controller , $options );
	}
    function group($url,$closure)
	{

		$url = preg_replace( '/\s*?/' , '' , $url );                  // stripes spaces
		$url = preg_replace( '/(.*)\[(.*)\](.*)/' , '$1$2$3' , $url );// stripes [ and ]
		$url = preg_replace( '/[\'"]/' , '' , $url );                 // stripes ' and "
		$url = preg_replace('/(.*?)(\/\.\.\.?\.?)/','$1',$url);       // stripes:   /...


		if( preg_match( '/.*?\|.*/' , $url ) ) //if it has middleware with |
		{
			$middles = preg_replace( '/.*\|(.*)/' , '$1' , $url );  // find the middlewares
			$options[ 'middleware' ] = explode( ',' , $middles );   // convert them into array

			$url = preg_replace( '/(.*)(\|.*)/' , '$1' , $url );    //detach the middleware from the end of action string
		}
		if( preg_match( '/.*?[@#&*].*/' , $url ) )
		{
			$options[ 'namespace' ] = preg_replace( '/.*?[@#&*](.*)/' , '$1' , $url );
			$url = preg_replace( '/(.*)([@#&*].*)/' , '$1' , $url );    //detach the middleware from the end of action string

		}
		$options['prefix'] = $url;
		app('router')->group($options,$closure);
	}
    function get( $uri , $action )
    {

        $options = parse_action( $action );

        return app( 'router' )->get( $uri , $options );
    }

    function post( $uri , $action )
    {

        $options = parse_action( $action );

        return app( 'router' )->post( $uri , $options );
    }

    function put( $uri , $action )
    {

        $options = parse_action( $action );

        return app( 'router' )->put( $uri , $options );
    }

    function patch( $uri , $action )
    {

        $options = parse_action( $action );

        return app( 'router' )->patch( $uri , $options );
    }

    function delete( $uri , $action )
    {

        $options = parse_action( $action );

        return app( 'router' )->delete( $uri , $options );
    }

    function parse_action( $action )
    {

        if( is_array( $action ) )
            return $action;

        $action = preg_replace( '/\s*?/' , '' , $action );                  //stripes spaces
        $action = preg_replace( '/(.*)\[(.*)\](.*)/' , '$1$2$3' , $action );//stripes [ and ]
        $action = preg_replace( '/[\'"]/' , '' , $action );                 //stripes ' and "


        $options[ 'middleware' ] = null;
        $options[ 'as' ]         = null;
        $options[ 'uses' ]       = null;

        if( preg_match( '/.*?\|.*/' , $action ) ) //if it has middleware
        {
            $middles = preg_replace( '/.*\|(.*)/' , '$1' , $action );
            $options[ 'middleware' ] = explode( ',' , $middles );

            $action = preg_replace( '/(.*)(\|.*)/' , '$1' , $action ); //detach the middleware from the end of action string
        }

        if( preg_match( '/.*?\(.*?\)/' , $action ) )// if it has route name in ()
        {
            $options[ 'as' ] = preg_replace( '/.*?\((.*?)\)/'   , '$1' , $action ); // find the routeName
            $action          = preg_replace( '/(.*?)\((.*?)\)/' , '$1' , $action ); // delete the routeName
        }

        $options[ 'uses' ] = $action;

        return $options;
    }