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

mstdmstd's avatar

How to call laravel route from js script file?

Hi all. In my larabel 5.7 I have in routes/web.php :

Route::group(['middleware' => ['auth', 'isVerified'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
    ...
    Route::get('get_system_info', [
        'as'   => 'get-system-info',
        'uses' => 'Admin\DashboardController@get_system_info']
    );

From my php code I can open this using "as" url from above as :

            return redirect()->route('admin.get-system-info');

But when I need to run this code in ajax from my .js file I make :

     var href= '/admin/get_system_info';
     $.ajax(
         {
             type: "GET",

My question is if there is a way to avoid using url "get_system_info" from my js file but to use "as" from my routes/web.php, like :

     var href= 'admin.get_system_info';
     $.ajax(
         {
             type: "GET",

?

If yes how ?

Thank you!

0 likes
4 replies
burlresearch's avatar

no - those 'named routes' in your routes files are for Laravel on the server-side. Javascript on the client-side does NOT have access to the mechanisms to decode these.

You're best left to use the URI field from php artisan route:listto form your href entries in JS.

$ php artisan route:list
+---------------+---------------------+------------------+----------------+--------------+
| Method        | URI                 | Name             | Action         | Middleware   |
+---------------+---------------------+------------------+----------------+--------------+
| POST          | api/markers         |                  | Closure     ...| api          |
| GET|HEAD      | api/subcat          | subcat.index     | App\Http\Con...| api          |
| POST          | api/subcat          | subcat.store     | App\Http\Con...| api          |
| GET|HEAD      | api/subcat/create   | subcat.create    | App\Http\Con...| api          |
| GET|HEAD      | api/subcat/{subcat} | subcat.show      | App\Http\Con...| api          |

2 likes
divyeshxd's avatar

thanks.. but how to use uri having parameters ?

mstdmstd's avatar

Thank you! Also a question if in 2 routes definitions below

// Route::get('contacts-us', 'ContactUsController@index')->name('contacts-us');   

Route::get('contacts-us', array(
    'as'      => 'contacts-us',
    'uses'    => 'ContactUsController@index'
));

contacts-us - is the same name of the route in diferent syntax?

Please or to participate in this conversation.