Should use Controller Argument
function view($post_id) {
...
}
or uri->segment
function view() {
...
$post_id = $this->uri->segment(3);
...
}
Just for simple blog post ID. Is there a difference?
$this->uri->segment(n) Segment function allow you to retrieve a specific segment form URI string where n is a segment number. Segments are numbered from left to right. For example,if your URI like. By the above example URI segment function give result by n parameter.
The URI Class provides methods that help you retrieve information from your URI strings. If you use URI routing, you can also retrieve information about the re-routed segments. This class is initialized automatically by the system so there is no need to do it manually.
Get URI Segments in PHP Use the following code to get URL segments of the current URL. It returns all the segments of the current URL as an array. $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
I would use the controller argument.
Then if you ever had to move the code and put it in a sub-directory it would still work, where as the segment method would not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With