patternphpMinor
Setting controller, action, and values based on the number of URL chunks
Viewed 0 times
thevaluesnumberchunksactioncontrollersettingbasedandurl
Problem
Is there any way to improve this code? It looks a bit ugly for me.
I thought about nested conditions, but this option is ugly as well.
if($url_chunks_num == 1) {
$this->controller = $this->pageData[0];
} elseif($url_chunks_num == 2) {
$this->controller = $this->pageData[0];
$this->action = $this->pageData[1];
} elseif($url_chunks_num > 2) {
$this->controller = $this->pageData[0];
$this->action = $this->pageData[1];
$this->values = array_slice($this->pageData, 2);
}I thought about nested conditions, but this option is ugly as well.
Solution
There seems to be some code repetition. Perhaps this could be improved like this:
Or
if ($url_chunks_num >= 1)
$this->controller = $this->pageData[0];
if ($url_chunks_num >= 2)
$this->action = $this->pageData[1];
if ($url_chunks_num > 2)
$this->values = array_slice($his->pageData, 2);Or
if ($url_chunks_num >= 1) {
$this->controller = $this->pageData[0];
if ($url_chunks_num >= 2)
$this->action = $this->pageData[1];
if ($url_chunks_num > 2)
$this->values = array_slice($his->pageData, 2);
}Code Snippets
if ($url_chunks_num >= 1)
$this->controller = $this->pageData[0];
if ($url_chunks_num >= 2)
$this->action = $this->pageData[1];
if ($url_chunks_num > 2)
$this->values = array_slice($his->pageData, 2);if ($url_chunks_num >= 1) {
$this->controller = $this->pageData[0];
if ($url_chunks_num >= 2)
$this->action = $this->pageData[1];
if ($url_chunks_num > 2)
$this->values = array_slice($his->pageData, 2);
}Context
StackExchange Code Review Q#12912, answer score: 7
Revisions (0)
No revisions yet.