Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Routing Annotation With Additional Prefix

URL = http://localhost/admin/user/login

/**
 * @RoutePrefix("/user")
 */
class UserController
{
    /**
     * @Get("/login")
     */
    public function loginAction() {}
}

Is their any other way that "admin" prefix will be automatically be added to the route prefix? I thought of this one solution however it doesn't suites to my application.

$add_prefix = 'admin';

$router->addModuleResource('Backend', 'User', $add_prefix . '/user');

Note: This "admin" prefilx can be change at the frontend.



2.2k
Accepted
answer
edited Feb '15

Hi Pavel, yes it can be done in Router Group, However in multi module it needs to have group routes class every module i create.. so this is what I've done in router annotations:

// $this->getDI( )->get( 'config' )->urls->prefixes->toArray( ); // contains
'prefixes'        => array(
    ':admin'  => 'admin',
    ':frontend'   => 'any/valid/url'
),
class Annotations extends \Phalcon\Mvc\Router\Annotations {

        public function processControllerAnnotation ( $handler, $annotation ) {

            $expr_arguments = $annotation->getExprArguments( );

            $url_prefixes   = $this->getDI( )->get( 'config' )->urls->prefixes->toArray( );
            $url_searchs    = array( );
            $url_values     = array( );

            foreach ( $url_prefixes as $url_search => $url_value ) {
                $url_searchs[]  = $url_search;
                $url_values[]   = $url_value;
            }

            foreach ( $expr_arguments as $key => $expr_argument ) {
                if ( isset( $expr_argument['expr']['value'] ) ) {
                    $expr_arguments[$key]['expr']['value'] = str_replace( $url_searchs, $url_values,  $expr_argument['expr']['value'] );
                }
            }

            $annotation = new \Phalcon\Annotations\Annotation( array(
                'name' => $annotation->getName( ),
                'arguments' => $expr_arguments
            ));

            return parent::processControllerAnnotation ( $handler, $annotation );

        }

    }

then in my controller i indicate

    /**
     * Router Prefix
     * @RoutePrefix( "/:admin/setting" )
     *
     */

    class SettingController extends \Phalcon\Mvc\Controller {

then i test it with

http://localhost/admin/setting http://localhost/admin/any/valid/url/setting (Note: change the :admin to frontend)

and it works... i don't know if this is the best idea but it solveds my problem and this is the simplest idea i up come with... thanks..

if you have any/better idea plss comment it out below...