Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:41:41+00:00 2026-06-17T17:41:41+00:00

My current router / FrontController is setup to dissect URL’s in the format: http://localhost/controller/method/arg1/arg2/etc…

  • 0

My current router / FrontController is setup to dissect URL’s in the format:

http://localhost/controller/method/arg1/arg2/etc...

However, I’m not sure how to get certain requests to default to the IndexController so that I can type:

http://localhost/contact
or
http://localhost/about/portfolio

Instead of:

http://localhost/index/contact
or
http://localhost/index/about/portfolio

How is this accomplished?

<?php

namespace framework;

class FrontController {
    const DEFAULT_CONTROLLER = 'framework\controllers\IndexController';
    const DEFAULT_METHOD     = 'index';

    public $controller       = self::DEFAULT_CONTROLLER;
    public $method           = self::DEFAULT_METHOD;
    public $params           = array();
    public $model;
    public $view;

    function __construct() {
        $this->model = new ModelFactory();
        $this->view = new View();
    }

    // route request to the appropriate controller
    public function route() {
        // get request path
        $basePath = trim(substr(PUBLIC_PATH, strlen($_SERVER['DOCUMENT_ROOT'])), '/') . '/';
        $path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), '/');
        if($basePath != '/' && strpos($path, $basePath) === 0) {
            $path = substr($path, strlen($basePath));
        }

        // determine what action to take
        @list($controller, $method, $params) = explode('/', $path, 3);
        if(isset($controller, $method)) {
            $obj = __NAMESPACE__ . '\\controllers\\' . ucfirst(strtolower($controller)) . 'Controller';
            $interface = __NAMESPACE__ . '\\controllers\\' . 'InterfaceController';
            // make sure a properly implemented controller and corresponding method exists
            if(class_exists($obj) && method_exists($obj, $method) && in_array($interface, class_implements($obj))) {
                $this->controller = $obj;
                $this->method = $method;

                if(isset($params)) {
                    $this->params = explode('/', $params);
                }
            }
        }
        // make sure we have the appropriate number of arguments
        $args = new \ReflectionMethod($this->controller, $this->method);
        $totalArgs = count($this->params);
        if($totalArgs >= $args->getNumberOfRequiredParameters() && $totalArgs <= $args->getNumberOfParameters()) {
            call_user_func_array(array(new $this->controller, $this->method), $this->params);
        } else {
            $this->view->load('404');
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:41:43+00:00Added an answer on June 17, 2026 at 5:41 pm

    You can use your URLs by one of two methods:

    Establish the controllers the way your routing defines them

    example.com/contact => Have a “contact” controller with default or index action

    example.com/about/portfolio => Have an “about” controller with a “portfolio” action

    Because your currently available routing says your URL is treated like “/controller/method”, there is no other way.

    Establish dynamic routing to allow multiple URLs to be handled by a single controller

    Obviously this needs a bit of configuration because one cannot know which URLs are valid and which one should be redirected to the generic controller, and which ones should not. This is somehow a replacement for any of the rewriting or redirecting solutions, but as it is handled on the PHP level, change might be easier to handle (some webserver configurations do not offer .htaccess because of performance reasons, and it generally is more effort to create these).

    Your configuration input is:

    1. The URL you want to be handled and
    2. The controller you want the URL passed to, and it’s action.

    You’ll end up having an array structure like this:

    $specialRoutes = array(
        "/contact" => "IndexController::indexAction",
        "/about/portfolio" => "IndexController::indexAction"
    );
    

    What’s missing is that this action should get the current URL passed as a parameter, or that the path parts become designated parameters within your URL schema.

    All in all this approach is a lot harder to code. To get an idea, try to look at the routing of common MVC frameworks, like Symfony and Zend Framework. They offer highly configurable routing, and because of this, the routing takes place in multiple classes. The main router only reads the configuration and then passes the routing of any URL to the configured routers if a match is detected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert string like this: $url = 'module/controller/action/param1/param1value/paramX/paramXvalue'; to url regarding current
Hi I'm setting up admin routing in CakePHP. This is my current route: Router::connect('/admin/:controller/:action/*',
I am submitting a normal <form method=get> element to the current url... It's part
With the current version of the ember router, you can define a route handler
I need to extract Voip log from a D-Link router, so I've setup a
Is there a way in the router to reset the url to the default
http://jsfiddle.net/pauldechov/u4naE/ App.Router = Em.Router.extend({ enableLogging: true, root: Em.Route.extend({ index: Em.Route.extend({ route: '/', connectOutlets: function(router)
In my current system I have a router service and over a dozen (about
I have a controller setup to accept two vars: /clients/view/var1/var2 And I want to
In my backbone.js, when I call model.save() the url gets the current URL path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.