change underscore to hyphen for url in php mvc

I am new to MVC and I have through some tutorials created a mini MVC framework using only php, no dependencies. The problem I have is that not all my pages are going to be just one word. A simple example would be 'contact-us'

I named my page in the views folder, 'contact_us.php' and I create this method in the controller:

public function contact_Us() {

 $data = [

     'title' => 'Contact us'

 ];

 $this->view('pages/contact_us', $data);

 }
If I navigate to 'example.com/pages/contact_us' then it works I see the content I should. But I don't want it to be contact_us, I want it to be contact-us

I am not sure if this needs to be done in the existing .htaccess file or if it is done on the core.php file. If anyone wants to see more code please let me know. I just didn't want to post everything here and waste space unnecessarily.
LVL 1
Black SulfurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
How about just

$this->view('pages/contact-us', $data);

Open in new window


alternatively, .htaccess will rewrite to a new page address, so if your actual page was contact-us.php  and you wanted to redirect traffic from contact_us.php to contact-us.php you could use this method as well (SEO purposes I presume?).
Black SulfurAuthor Commented:
I think the change needs to be in my core.php file which I didn't post in my initial question:

class Core {
	
	protected $currentController = 'Pages';
	protected $currentMethod = 'index';
	protected $params = [];
	
	public function __construct() {
		$url = $this->getUrl();
		
		// Look in Controllers for first value
		if(file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
			//if exists, set as controller
			$this->currentController = ucwords($url[0]);
			// Unset zero index
			unset($url[0]);
		}
		
		// Require the controller
		require_once '../app/controllers/' . $this->currentController . '.php';
		
		// Instantiate controller
		$this->currentController = new $this->currentController;
		
		// Check for second part of url
		if(isset($url[1])) {

			// check to see if method exists in controller
			if(method_exists($this->currentController, $url[1])) {
				$this->currentMethod = $url[1];
				// Unset 1 index
				unset($url[1]);
			}	
		}
		
		// Get params
		$this->params = $url ? array_values($url) : [];
		
		// Call a callback with array of params
		call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
	}
	
	public function getUrl() {
		if(isset($_GET['url'])) {
			$url = rtrim($_GET['url'], '/');
			$url = filter_var($url, FILTER_SANITIZE_URL);
			$url = explode('/', $url);
			return $url;
		}
	}
}

Open in new window

NerdsOfTechTechnology ScientistCommented:
I named my page in the views folder, 'contact_us.php'

what if you renamed this file contact-us.php then navigate to 'example.com/pages/contact-us' ?
Black SulfurAuthor Commented:
I changed it back to contact-us.php and added this to the core.php file

if (preg_match('/^[a-z]+-[a-z]+$/i', $url[1])) {
 				$url[1] = str_replace('-', '_', $url[1]);
  			}

Open in new window


That seems to have done the trick.
NerdsOfTechTechnology ScientistCommented:
If you really need to test for '-', strpos() performs better than preg_match()
if (strpos($url[1], '-') !== FALSE) { // catch any url with -
 $url[1] = str_replace('-', '_', $url[1]);
}

Open in new window

since str_replace is conditional already why not just do the replace outright in one line without the IF?:
$url[1] = str_replace('-', '_', $url[1]);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.