Welcome Guest, Not a member yet? Register   Sign In
Question regarding the uri + more
#1

[eluser]Dennis_gull[/eluser]
Hello everyone,

I'm new to codeigniter and this community so first of all I would like to thank you guys for this excellent MVC framework Smile

Now when I have played around with it for a couple of days I have a few questions I would like to ask:

1) URI - I understand that the URI uses <optional folder>/class/function/variables so lets say I create a controller named search and I want the user to be able to type example.com/search/<search variable> to get a search result, meaning the index page (function) is the search result page, but CI expect the second parameter to be the function and therefore I have to write example.com/search/index/<search variable>, is it somehow possible to skip the index in the url to get a more clean url?

2) GET forms - This question is related to the first one, lets say I create a search form with a GET method, when i submit this form I get in return a normal query string like this example.com/search?id=<my_search> instead of example.com/search/<my_search>. Is there some option to enable seo URIs on GET forms?

3) sessions - I feel a little "unused" to the CI sessions, I understand that the session is stored in the cookies but shouldn't the session end once the user close the browser? For now the session is kept even if you close the browser.

4) I would like to automatically run some lines of code before I initiate any script in an controller, is this possible without changing any of the "core" files?

I will probably have more questions later but I think this will do for now.
Thanks in advance!
#2

[eluser]jedd[/eluser]
Hi Dennis and welcome to the CI forums.

Quote:1) URI - I understand that the URI uses <optional folder>/class/function/variables so lets say I create a controller named search and I want the user to be able to type example.com/search/<search variable> to get a search result, meaning the index page (function) is the search result page, but CI expect the second parameter to be the function and therefore I have to write example.com/search/index/<search variable>, is it somehow possible to skip the index in the url to get a more clean url?

There are several things you can search in the user guide - ROUTES, REMAP, REDIRECT - but you are probably approaching the problem the wrong way. As in, you are unlikely to have a controller called search - your controller would be named after a resource that your site lets you manage, and one of the functions in there would be search.

Quote:2) GET forms - This question is related to the first one, lets say I create a search form with a GET method, when i submit this form I get in return a normal query string like this example.com/search?id=<my_search> instead of example.com/search/<my_search>. Is there some option to enable seo URIs on GET forms?

We don't like GETs, and much prefer POSTs.

You can enable the query string style of URI's if you want - search the user guide for QUERY STRING.


Quote:3) sessions - I feel a little "unused" to the CI sessions, I understand that the session is stored in the cookies but shouldn't the session end once the user close the browser? For now the session is kept even if you close the browser.

Unencrypted sessions shouldn't contain sensitive data. If you have a a situation where you want better security, you'd encrypt the session. There are several threads in the forums on different approaches to doing what you want to do here, as the question pops up regularly. And yes, it does appear to be a feature we could do with in the core.

Quote:4) I would like to automatically run some lines of code before I initiate any script in an controller, is this possible without changing any of the "core" files?

Search the user guide for MY_CONTROLLER - it's the answer to all your prayers.
#3

[eluser]Dennis_gull[/eluser]
Thank you for the answers, I have some follow ups:

Quote:Question 1 ...
There are several things you can search in the user guide - ROUTES, REMAP, REDIRECT - but you are probably approaching the problem the wrong way. As in, you are unlikely to have a controller called search - your controller would be named after a resource that your site lets you manage, and one of the functions in there would be search.

I'm not sure what you mean by resources (just started using MVC)?
From what I understand I use a controller for each page, for example:
register
register/submit
register/error
register/<another subpage (function)>

Quote:Question 2...
We don't like GETs, and much prefer POSTs.

You can enable the query string style of URI's if you want - search the user guide for QUERY STRING.

But I rather have human friendly URI strings instead of a normal query string, the GET form gives me a normal query string instead of the human friendly string.
I understand that you guys don't like GETs, but sometimes they are necessary Smile

Quote:Question 3...
Unencrypted sessions shouldn't contain sensitive data. If you have a a situation where you want better security, you'd encrypt the session. There are several threads in the forums on different approaches to doing what you want to do here, as the question pops up regularly. And yes, it does appear to be a feature we could do with in the core.

What I want to store in the session is the user id and username, but some users don't want to auto login when they visit the site. I found a library in the wiki called Native session, should I use this instead?
#4

[eluser]Michael Wales[/eluser]
Quote:I’m not sure what you mean by resources (just started using MVC)?
From what I understand I use a controller for each page, for example:
register
register/submit
register/error
register/<another subpage (function)>


Using jedd's mention of a resource, and your example above, go through the following thoughts:
- What is registering?
- What else will whatever is registering need to do?

Hopefully you go down this line of thinking:
- Accounts are being registered
- Accounts will need to register, retrieve forgotten passwords, login and logout

accounts/register
accounts/retrieve-password
accounts/login
accounts/logout

Quote:register/submit
We usually self-submit forms. You display a form on a conditional basis (if validation did not pass), submit the form to itself where it will check for validation and then either display the form again (on failure) or do database stuff (and then redirect somewhere else, to prevent double-submit).

Quote:register/error
Very rarely will you need a function just dedicated to displaying errors. Running with the form example above: when validation fails you would dump the reasons for failure into a variable, then in your view - if that variable exists, display the content.
#5

[eluser]Dennis_gull[/eluser]
Thanks for clearing that one out.
#6

[eluser]Dennis_gull[/eluser]
Any hints on question 2 & 3?

Before I decided to convert to CI i used the below code in the htaccess to change the GET form result url to a human friendly url, is this my only option?
Code:
RewriteCond %{REQUEST_URI} search.php$
RewriteCond %{QUERY_STRING} ^id=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ search/%1? [R=301,L]
RewriteRule ^search/(.*)$ search.php?id=$1&a=1 [L]
#7

[eluser]Michael Wales[/eluser]
2) The most common way is to have a method that is just designed to accept a form submission (via POST) and redirect to another method that will handle that search. Something like this:

Code:
function search($query = NULL) {
  if ($this->input->post('q')) {
    redirect('search/' . $this->input->post('q'));
    return;
  }

  if ($query !== NULL) {
    // Do your search and display results
  }
}

You would then also need to define a route to translate any request to search/term to search/search/term - easy stuff and you get your linkable SEO friendly search results.


3) CodeIgniter (out of the box) does not support the ending of a session on browser close, but there are numerous posts around the forums that will instruct you on how to enable this functionality. Switching to another session library is a bit overkill to enable this really basic functionality.
#8

[eluser]Dennis_gull[/eluser]
Thanks a lot for all your help Michael Wales Smile




Theme © iAndrew 2016 - Forum software by © MyBB