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

How can I use USERNAME in url like facebook?

I am trying to use username in url like site.com/username in phalcon and I don't know how to get it. And also I want to know that if someone try to visit site.com/noUser and if system can't find "noUser" in database then to show error 404 page.

My public/index.php file has these codes

    // Create the router
    $router = new \Phalcon\Mvc\Router();

    //Define a route
    $router->add(
        "/([a-zA-Z0-9_]+)",
        array(
            "controller" => "user",
            "action"     => "profile",
            "name"       => 1,
        )
    );

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

and my user controller has these codes but I am getting 404 error when I try to echo username from url like site.com/username

class UserController extends ControllerBase
{

    public function profileAction(){
        $name = $this->dispatcher->getParam("name");
        echo  $name;
    }
}

Please let m know how can I confgure this.

I think you just forget the $router->handle(); in your index file

I tried and it didn't work either.



9.3k
Accepted
answer
edited Nov '14

Yea it looks you use the false regex

you use the regex for a action:[a-zA-Z0-9_]+

For a param is (/.*)*

Or you can try to change your action routing to

$router->add(
    "(/.*)*",
    array(
        "controller" => "user",
        "action"     => "profile",
        "param"         => 1,
    )
);

It is still not working.

Yea it looks you use the false regex

you use the regex for a action:[a-zA-Z0-9_]+

For a param is (/.*)*

Or you can try to change your action routing to $router->add( "(/.*)*", array( "controller" => "user", "action" => "profile", "param" => 1, ) );



98.9k

You have to disable the default routes:

$router = new \Phalcon\Mvc\Router(false);

This worked, but now I will have to define all routs by myself. Thanx.

You have to disable the default routes:

$router = new \Phalcon\Mvc\Router(false);

This worked, but now I will have to define all routs by myself. Thanx.

Yea it looks you use the false regex

you use the regex for a action:[a-zA-Z0-9_]+

For a param is (/.*)*

Or you can try to change your action routing to

$router->add(
   "(/.*)*",
   array(
       "controller" => "user",
       "action"     => "profile",
       "param"         => 1,
   )
);

it worked, but now in my routers, I have these codes which are not working anymore.

$router->add( "/accounts/login", array( "controller" => "accounts", "action" => "login", ) );

$router->add( "/accounts/signup", array( "controller" => "accounts", "action" => "signup", ) );

$router->add( "/accounts/forgotpass", array( "controller" => "accounts", "action" => "forgotpass", ) );

You have to disable the default routes:

$router = new \Phalcon\Mvc\Router(false);
edited Nov '14

So, my public/index.php file has these codes now,

<?php

error_reporting(E_ALL);

try {

/**
 * Read the configuration
 */
$config = include __DIR__ . "/../app/config/config.php";

/**
 * Read auto-loader
 */
include __DIR__ . "/../app/config/loader.php";

/**
 * Read services
 */
include __DIR__ . "/../app/config/services.php";

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

//trying to display error pages
$di->set('dispatcher', function() {

$eventsManager = new \Phalcon\Events\Manager();

$eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

        //Handle 404 exceptions
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $dispatcher->forward(array(
                'controller' => 'error',
                'action' => 'show404'
            ));
            return false;
        }

        //Handle other exceptions
        $dispatcher->forward(array(
            'controller' => 'error',
            'action' => 'show503'
        ));

        return false;
    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;

}, true);

$di->set('router', function() {

    require __DIR__ . '/../app/config/routes.php';  

    return $router;
 });

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

} catch (\Exception $e) { echo $e->getMessage(); }

and my routes.php file has these codes,

<?php

$router = new \Phalcon\Mvc\Router(false);

// default

$router->add("/", array( 'controller' => 'index', 'action' => 'index' ));

//Set 404 paths

$router->notFound(array( "controller" => "error", "action" => "show404" ));

//adding accounts routes

$router->add( "/accounts", array( "controller" => "accounts", "action" => "index", ) );

$router->add( "/accounts/login", array( "controller" => "accounts", "action" => "login", ) );

$router->add( "/accounts/signup", array( "controller" => "accounts", "action" => "signup", ) );

$router->add( "/accounts/forgotpass", array( "controller" => "accounts", "action" => "forgotpass", ) );

//adding users

$router->add("(/.)", array( "controller" => "user", "action" => "profile", "param" => 1, ));

$router->handle(); return $router;

and when i try to access localhost/accounts/login, it is printing /accounts/login instead of showing me loginaction.

try set defaultNamespace

$router->setDefaultNamespace('\Controllers');

Or You can $router->add( "/accounts/forgotpass", array( 'namespace' => 'Your_NAMESPACE', "controller" => "accounts", "action" => "forgotpass", ) );

I had problem when all actions showed 404 page, I did this

$router = new \Phalcon\Mvc\Router(false);
$router->setDefaultNamespace('\Controllers');
$router->removeExtraSlashes(true);

Not working. :'(

try set defaultNamespace

$router->setDefaultNamespace('\Controllers');

Or You can $router->add( "/accounts/forgotpass", array( 'namespace' => 'Your_NAMESPACE', "controller" => "accounts", "action" => "forgotpass", ) );

So when you try your not working routes, do you land on your 404 action?

Ok. This is my Di

    $di->set('router', function() {
        $router = new \Phalcon\Mvc\Router(false);
        $router->setDefaultNamespace('\Controllers');
        $router->removeExtraSlashes(true);
        include ROOT_DIRECTORY . 'config'.DIRECTORY_SEPARATOR.'routes.php';
        return $router;
},true);

Where routes.php look like as

    router->addPost('/api/user/login', array(
        "controller" => "user",
        "action" => "login"
    ));

Thanx for sharring your codes, I found my simple way, I changed username function like this,

$router->add("/~{name}", array( "controller" => "user", "action" => "profile", "param" => 1, ));

and now everything is working good.

Ok. This is my Di

  $di->set('router', function() {
      $router = new \Phalcon\Mvc\Router(false);
      $router->setDefaultNamespace('\Controllers');
      $router->removeExtraSlashes(true);
      include ROOT_DIRECTORY . 'config'.DIRECTORY_SEPARATOR.'routes.php';
      return $router;

},true);

Where routes.php look like as

  router->addPost('/api/user/login', array(
      "controller" => "user",
      "action" => "login"
  ));