Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zachleigh's avatar

Routing Ajax Requests

Whats the best way to route AJAX requests? Im currently routing each request to its own route, but this is quickly becoming messy.

0 likes
4 replies
jekinney's avatar

You can separate your routes into different files and import them into the main route.php.

Other then that generalizing the routes (not being explicitly set) has some possible security issues and if it's for a client good chance on getting that call at 0300 because of an error.

1 like
jekinney's avatar

Phone, so double post but the controller method on l5 like the default auth routes maybe what your looking for, but head the eating above.

zachleigh's avatar

Thanks for the tips. At least for now, I decided to prefix my ajax requests and keep them all in a separate file. Cleaned things up a bit.

ImanGh's avatar

use this helper functions they may help you a little bit... they allow you to write :

  • ( it does not break the default syntax )
    get( '/url'   , 'HomeController@index (routeName)  | auth,logger ');  //instead of :
    get( '/'  , ['as'=>'routeName' , 'uses'=>'HomeController@index' , 'middleware'=>['auth' , 'logger']]); // would be still valid

note that these are treated as the same

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

(put them in a some-file.php and require the in your bootstrap/autoload.php BEFORE the autoload.php) require DIR.'/../some-file.php'; require DIR.'/../vendor/autoload.php';

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;
    }

Please or to participate in this conversation.