We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Uri for a domain alias

Suppose I have a site exempleone.com that is running a website with phalcon. And I have an alias for that domain that is exone.com

I want to create an URI for that alias that every time someone types exone.com/?XyzT where XyzT is a unique code for a user, it would redirect for a specific controller/action.

How can I do that?

Thanks, Eduardo



98.9k
edited Aug '14

I think it would be hard to create a redirection for a url like exone.com/?XyzT but it would be easier if the url is: exone.com/c/XyzT, you can create a route like:

$router->add(
    '/c/{[a-z0-9]+}',
    array(
       'controller' => 'code',
       'action'     => 'handleCode'
    )
);
class CodeController extends Controller
{

    public function handleCodeAction($code)
    {
        //...
    }
}


4.0k
edited Aug '14

How can i put it in my index bootstrap file?

Here is my index.php

//some more code above this line....

//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function() {
    $url = new \Phalcon\Mvc\Url();
    //$url->setBaseUri('/'); para uso produção
    $url->setBaseUri('/link2me/'); //para uso local
    return $url;
});

if ($_SERVER["SERVER_NAME"] == 'l-2.me') {
        $router = new Phalcon\Mvc\Router();

        $router->add(
            '/s/{[a-z0-9]+}',
            array(
               'controller' => 'index',
               'action'=> 'search'
        ));

        $router->handle();
}

//Handle the request
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

But when i try to open the url i got the following: Phalcon Error:. SController handler class cannot be loaded



98.9k
edited Aug '14

You have to add the router to the DI:

$di->set('router', function() {
    $router = new Phalcon\Mvc\Router();

    $router->add(
        '/s/{[a-z0-9]+}',
        array(
            'controller' => 'index',
            'action'=> 'search'
    ));

    return $router;
});


4.0k

I did it before read your post but i'm keeping getting the same error:

SController handler class cannot be loaded



4.0k

@Phalcon...

Any idea to solve this problem?



4.0k
Accepted
answer

It worked for me using the following way:

$router->add(
    '/s/{code}',
    array(
        'controller' => 'index',
        'action'=> 'search'
));

return $router;
});