CodeIgniter Forums
Im just confused how to build this URL (easy question for you guys, I think) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Im just confused how to build this URL (easy question for you guys, I think) (/thread-53500.html)



Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-26-2012

[eluser]towki[/eluser]
Please see this image, I cannot attach files

My method/function "administration" in my "Infosystem" controller loads a view that serves as an administration page that is only accessible by admins.

For example an admin logged in typed "www.example.com/infosystem/administration" my administration page will be showed up consisting of several administrative functions.

Speaking of this administrative function, My problem exactly is I want to have a link on this administration page that point to another view/page that function as managing the users,
and that link should have a URL like this
"www.example.com/infosystem/administration/user_management".

If not for my URL concern, I could have created my user_management page by creating another function in my controller and loading a view. but i think that will not create the URL that I want.

Just a newbie. Please help me with this







Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-26-2012

[eluser]pickupman[/eluser]
You would need to use the [url="http://codeigniter.com/nightly_user_guide/general/routing.html"]Router[/url] class for this. Routing is for making a uri use a different pattern than the standard in CI. This would go in /application/config/routes.php
Code:
$route['infosystem/administration/user_management'] = 'infosystem/user_management/'; //URI without 4th segment
$route['infosystem/administration/user_management/(:any)'] = 'infosystem/user_management/$1'; //URIs with 4 or more segments



Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-26-2012

[eluser]TWP Marketing[/eluser]
Since your second URL segment, "administration", may be used for two (or more) functions, you can set up a conditional statement in the administration method code:

Your Controller named Infosystems"
Code:
...
function administration($func="admin")
{
if( $func == "user_management" )
{
  $this->user_management(); // call the user management method in this controller
  return; // leave, done with user_management function
}
// put the default administation stuff here
}

function user_management()
{
// do you user management stuff here
}
...

Now if you use this URL:
Code:
http://www.example.com/infosystems/adminstration
The value of the third URL segment is blank and the controller function will use the default value for $func = "admin" (or whatever you want to make it).

If you use this URL:
Code:
http://www.example.com/infosystems/adminstration/user_management
The value of $func will be set to the third URL segment "user_management"
and the conditional statement will call the user_management method and then return.

NOTE: CI will pass the third URL segment as a parameter of the second URL segment, it's built in to CI.


Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-26-2012

[eluser]towki[/eluser]
@TWP MARKETING, I have tried your code and it works. However
when I typed this url:

Code:
http://www.example.com/infosystems/user_management

The user_management function can still be accessed. I want this user_management to be accessed only by the URL i wanted.

Code:
http://www.example.com/infosystems/adminstration/user_management

Because this user_management is under the administration so its better to see my URL like that and could only be accessed by that URL


Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-27-2012

[eluser]pickupman[/eluser]
Don't you think it would be tough for some to guess that the URI might be different an remove a segment to access the page from the real URI. You know it's an option, but no one else would. It's also unlikely that anyone not in web development even pays attention to a url, let alone manipulate it.

With that being said, just make it a private method to make it inaccessible directly.
Code:
public function administration($func="admin")
{
if( $func == "user_management" )
{
  $this->_user_management(); // call the user management method in this controller
  return; // leave, done with user_management function
}
// put the default administation stuff here
}

private function _user_management()
{
// do you user management stuff here
}



Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-27-2012

[eluser]towki[/eluser]
[quote author="pickupman" date="1343396461"]Don't you think it would be tough for some to guess that the URI might be different an remove a segment to access the page from the real URI. You know it's an option, but no one else would. It's also unlikely that anyone not in web development even pays attention to a url, let alone manipulate it.

With that being said, just make it a private method to make it inaccessible directly.
[/quote]

Thanks for reply.
But I have another question about URL. Sorry if my issues was all about URL

When i typed my url for example and add another segment at the end of it which do not exist in my content, it should show an error 404 page. But instead it shows the page for the last valid segment in the URL.How could I fix that??.



Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-27-2012

[eluser]pickupman[/eluser]
I supposed if you wanted to worry about it, something like this would take care of it.
Code:
function administration($func="admin")
{
if( $func == "user_management" )
{
    if( ! is_null($this->uri->segment(4))) show_404(); //if 4th segment is not NULL show 404 error

    $this->user_management(); // call the user management method in this controller
    return; // leave, done with user_management function
}
// put the default administation stuff here
}



Im just confused how to build this URL (easy question for you guys, I think) - El Forum - 07-27-2012

[eluser]TWP Marketing[/eluser]
Quote:...
Thanks for reply.
But I have another question about URL. Sorry if my issues was all about URL

When i typed my url for example and add another segment at the end of it which do not exist in my content, it should show an error 404 page. But instead it shows the page for the last valid segment in the URL.How could I fix that??.
If there may be another segment, which is optional, just parse it the same as the prior segment.
For a URL like this:
Code:
http://mydomain.com/infosystems/administration/user_management/some_var
The fourth segment "some_var" will be available as the second parameter of the controller function administration() which I have arbitrarily called "$something".
Code:
...
function administration($func="admin", $something="" ){
if( $func == "user_management" )
{
  if( $something != "" )
  {
    // process the "$something" var as necessary
    // for instance you code do show_404()
  }
  $this->user_management(); // call the user management method in this controller
  return; // leave, done with user_management function
}
// put the default administation stuff here
}

function user_management()
{
// do you user management stuff here
}
...