I am building a single-page-app using vuejs as frontend, handling routes, templates… etc, and yii rest as backend.

Lets say i have these urls:

GET "/users" and GET "/users/123"

TL;DR : if is an ajax request run restful controller/action, if not run web defaultController/index just to return html page then views are handled by js by calling restful controller/action.

I am thinking of implementing this in "beforeAction" or web config or something like that.

Is this methodology right? or there is a better option?

Longer verison:

The goal is having working urls and have only one source of views "vuejs" instead of duplicating.

if requested via ajax return normal restful response,

if it is not an ajax request call a default action instead "site/index" and return simple html page contain

necessary js, then vue js take it from there. parsing uri and get templates, data and handle 404 requests… etc

This is how I do it:


namespace app\components;

use Yii;

class UrlManager extends \yii\web\UrlManager



     * @var array Routes that should be processed through yii instead of the frontend client

     *            These must appear at the beginning of the string


    public $yiiRoutes = [

        "v1",      // api v1

        "debug",   // debug module

        "gii",     // gii module



     * @var string Frontend client route


    public $frontendRoute = "app/index";


     * @inheritdoc


    public function parseRequest($request)


        // check if we're calling a route that should be processed by yii

        $pathInfo = $request->getPathInfo();

        foreach ($this->yiiRoutes as $yiiRoute) {

            // check for direct route or a prefixed route, eg, "gii" or "gii/*"

            $yiiRoutePrefix = "$yiiRoute/";

            if ($pathInfo === $yiiRoute || strpos($pathInfo, $yiiRoutePrefix) === 0) {

                return parent::parseRequest($request);



        // use frontend route


        return parent::parseRequest($request);



Thanks amnah. So frontend and api URLs don’t have to be the same, implementing in URL manager is good idea