Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Working Option] Controllerless URLs - mydomain.com/page #704

Closed
videsignz opened this issue Aug 20, 2015 · 9 comments
Closed

[Working Option] Controllerless URLs - mydomain.com/page #704

videsignz opened this issue Aug 20, 2015 · 9 comments

Comments

@videsignz
Copy link

Just wanted to throw something out here....

This is for those of you that want mydomain.com/page
instead of mydomain.com/index/page (no controller displayed)

The great thing is that everything is still kept inside the framework and still uses controllers and actions to render everything. And even better, 404 page detection works as expected.
It is completely non intrusive to the framework and only one function in one file needs to modified.

The default controller, which we get from Config::get('DEFAULT_CONTROLLER'), is the only one that will hide the controller name when using mydomain.com/page yet still works normal when given mydomain.com/default_controller/page...like I said, non intrusive :)

The logic simplified

mydomain.com acts like normal
index/index or default_controller/default_action

mydomain.com/login/showProfile again acts like normal
login/showProfile or given_controller/given_action

mydomain.com/page this is the good stuff
/page is actually index/page or default_controller/given_action

Here is the script which replaces the current

private function createControllerAndActionNames(){}

in the core/application.php file.

    private function createControllerAndActionNames()
    {
        // If no controller or action name is given, use defaults 'index/index'
        if (!$this->controller_name AND (!$this->action_name OR (strlen($this->action_name) == 0))) {
            $this->controller_name = Config::get('DEFAULT_CONTROLLER');
            $this->action_name = Config::get('DEFAULT_ACTION');
        }       

        // If a controller name is given, but an action is not
        if ($this->controller_name AND (!$this->action_name OR (strlen($this->action_name) == 0))) {
            // Does such a controller exist ?
            if(file_exists(Config::get('PATH_CONTROLLER') . ucwords($this->controller_name) . 'Controller.php')){
                // If yes, set the action to default 'this-controller/index'
                $this->action_name = Config::get('DEFAULT_ACTION');
            }else{
                // And the magic...set the action to the original controller given 'index/controller becomes action'
                // This works since no action was originally given, we assume that the controller is the action!
                // Mind you, we already checked to make sure the controller does not actually exist so we are good to go!
                $this->action_name = $this->controller_name;
                // Lastly, set the controller to default 'index/new action'
                $this->controller_name = Config::get('DEFAULT_CONTROLLER');             
            }
        }       

        // rename controller name to real controller class/file name ("index" to "IndexController")
        $this->controller_name = ucwords($this->controller_name) . 'Controller';
    }

Take some time to look through it...and let me know your thoughts or questions! You can easily give it a try as well, just replace the function and visit a page rendered by and action in your default controller like mydomain.com/action

I should note that given controller names will always take precedence over page name, this is a must!
ie. if 'dashoard' is a controller and also page name, and you type mydomain.com/dashboard the result will be mydomain.com/dashboard/default_action so no worries there!

@Dominic28
Copy link
Contributor

The index method is called automatically when you call something like domain.tld/contact.
So why don't you create a controller and put your stuff inside of the index method? This is working right out of the box.

@videsignz
Copy link
Author

@Dominic28 I am not quite sure what you mean? All I am doing is adding methods to the IndexController to render the views in which I would like 'index/' to be hidden...Can you explain a bit how this is 'out of the box'?

@Dominic28
Copy link
Contributor

Ah, I see you don't want to use a controller. But it would be possible to do this with controllers and without changes to the core of huge.

Can you give me an usecase for your solution?

@videsignz
Copy link
Author

@Dominic28 Actually, I do (and am) using IndexController. The goal was to hide the controller name within the url and only show the page name. ie. mydomain.com/contact when normally it would be mydomain.com/index/contact

It is a legibility and ease of remembering concern I had and wanted to implement. I surely am cool with other controller names showing up in the url as these are unique sections and make users feel more comfortable knowing where they are...like mydomain.com/myaccount/profile

When it comes to pages like about, contact, services and things like that, I feel its more of a standard to have them showing at the base url. Easier for users to remember and access.

While being new to the idea of using the mvc, I wanted to make sure that all my pages are rendered via a controller so I have access to all the functions within that app, yet wanted to segregate a set of pages to show at the base url. The above script recognizes mydomain.com/contact processes it using the index controller yet does not show mydomain.com/index/contact

@Dominic28
Copy link
Contributor

Ok I got that :)
I created pages like /contact by creating a controller called ContactController and output the page with the index method.

The script itself calls index automatically.
So /contact is actually /contact/index but you only see /contact.

@videsignz
Copy link
Author

@Dominic28 Gotcha! I had considered doing that too but I thought it would get cumbersome if I had a lot of pages. Keeping all the pages I want to show up like that within one controller and one view folder seemed like the way to go :)

@panique
Copy link
Owner

panique commented Oct 11, 2015

Hey, this is indeed an interesting feature, but I think it's too much work, this will need some rework of the core and a lot of testing, I don't have the time to do this so please let's close this ticket and put it on the "potential features" list in the readme.

@panique panique closed this as completed Oct 11, 2015
@nolsen89
Copy link

i got a problem, with this!
i created index/article, but when i include a param to the_action it doesnt work.
example /article/12/

@cristopher
Copy link
Contributor

cristopher commented Dec 24, 2020

i got a problem, with this!
i created index/article, but when i include a param to the_action it doesnt work.
example /article/12/

pfff...

you must read the code carefully

according:
application/config/config.development.php

     'DEFAULT_CONTROLLER' => 'index',
     'DEFAULT_ACTION' => 'index',

when you create a controller called news
you must create an index function

class NewsController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public static function index()
    {
        $this->View->render('news/index', array('news' => NewsModel::getNews()));
    }
}

when you surf the internet
localhost/news

the php loads the news controller and the default index function.

if you want to send an argument to the index function you must load from the browser
localhost/news/index/args

if you want to send arguments to another function called articles
you must do:
localhost/news/articles/12

and the controller

    public static function index()
    {
        $this->View->render('news/index', array('news' => NewsModel::getNews()));
    }

    public static function articles($n_article = 0)
    {
          if ($n_article != 0 && is_numeric($n_article)) {
               $this->View->render('news/article', array('article' => NewsModel::getArticle($n_article)));
          }else{
            Redirect::home();
         }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants