Welcome Guest, Not a member yet? Register   Sign In
Load default but dynamic view in any controller?
#1

[eluser]Sky Warden[/eluser]
Hello everyone. I'm pretty new to Code Igniter and haven't learned many things about it, so please help me.

I'm making a website which has the same template for any pages. Like a page to show video, read articles, etc. All pages have the same template. It will have a header, a left and top navigation. The left navigation and the header will show different values, depending on the page. Like if it's the home page, the header will print "Home." Just like this in page controller :

Code:
function index($page = 'home')
{
  $data['title'] = ucfirst($page);
  $this->load->view('templates/template_header', $data);
                $this->load->view('templates/template_banner');
                $this->load->view('templates/template_left_navigation', $data);
                $this->load->view('pages/'.$page, $data);
                $this->load->view('templates/template_footer');
        }

The problem is, I have to retype those codes manually in other controllers. Like for the registration controller and upload_video controller. The login form is on the header, so the user can login from any page. The form validation error can't be shown properly as well. I think I need a separated controller and view to do that. That's also the problem.

What I want is to show the login form on every page. The login form will send the input to login controller. Login controller will process that input, and send validation error if there's any. The login controller load no view other than the templates (login form, etc). After that, the user is redirected to the previous page he visited.

Hmm. Pretty confusing. Let me give an example :
1. User visit about page.
2. It loads templates, which includes the login form, and the view('pages/about');
3. User use the login form.
4. The login form sends the input to login_controller.
5. The login_controller process the form, and show any error and the view('pages/about');

I want the templates to be loaded automatically in any controller, so the controllers will only about managing the content, like showing search resuly. How can I do that? The last step is also pretty confusing to me.

Thanks for reading.
#2

[eluser]TheFuzzy0ne[/eluser]
The most logical way to do this in my mind, is pretty much as you are doing now, but have a dedicated login page. Every time a page loads, you can add a hidden form field to your login form at the top of the page, which contains the current URL. Just be sure not to set this hidden form field on the login page itself. To do this, you can check to see if the redirect URL has been posted, if it has, use that, if it hasn't, use the current URL.

When the user logs in, they submit the data to the login page. If there are any errors, they are displayed on the login page. Once the user logs in successfully, they are then redirected back to the page they were on before. Piece of cake, no?

On top of this, you could also create a modal login form with some Javascript.
#3

[eluser]Sky Warden[/eluser]
[quote author="TheFuzzy0ne" date="1363700904"]The most logical way to do this in my mind, is pretty much as you are doing now, but have a dedicated login page. Every time a page loads, you can add a hidden form field to your login form at the top of the page, which contains the current URL. Just be sure not to set this hidden form field on the login page itself. To do this, you can check to see if the redirect URL has been posted, if it has, use that, if it hasn't, use the current URL.

When the user logs in, they submit the data to the login page. If there are any errors, they are displayed on the login page. Once the user logs in successfully, they are then redirected back to the page they were on before. Piece of cake, no?

On top of this, you could also create a modal login form with some Javascript.[/quote]

Thanks for the reply. I can't think about something more logical neither. At least not at the moment.

Yes, yes. I know about hidden form. Though I'm still thinking about making the login form shown on any page.

By the way, how about the auto template? It's not really a big issue, but I believe there's a better way than writing the $this->load->view in every controllers. I've read a little about extending the main class, but haven't got a grip on it since I can't find a good example on how to use it.
#4

[eluser]TheFuzzy0ne[/eluser]
[quote author="Sky Warden" date="1363703254"]
Yes, yes. I know about hidden form. Though I'm still thinking about making the login form shown on any page.
[/quote]

With what I was suggesting, you can have a login on each page. The dedicated login controller would only be visible if the login information was invalid. As soon as it's corrected, it would redirect back to the right page, where the login form will no longer be visible (since they'd be logged in).

[quote author="Sky Warden" date="1363703254"]
By the way, how about the auto template? It's not really a big issue, but I believe there's a better way than writing the $this->load->view in every controllers. I've read a little about extending the main class, but haven't got a grip on it since I can't find a good example on how to use it.[/quote]

I'm not entirely sure what you mean. Your controller needs to know which view to load, so a call to $this->load->view() in each controller method is expected.

You can have a main template, and set it up something like this:

./application/views/main_template.php
Code:
$this->load->view('header_view');
$this->load->view($main_content);
$this->load->view('footer_view');

So from your controller method, you'd load your view like this:
Code:
$this->load->view('main_template', array(
    'main_content' => 'some_view',
));

I leave it to your imagination what might go into header_view.php and footer_view.php. Smile
#5

[eluser]Sky Warden[/eluser]
[quote author="TheFuzzy0ne" date="1363703809"]

With what I was suggesting, you can have a login on each page. The dedicated login controller would only be visible if the login information was invalid. As soon as it's corrected, it would redirect back to the right page, where the login form will no longer be visible (since they'd be logged in).

I'm not entirely sure what you mean. Your controller needs to know which view to load, so a call to $this->load->view() in each controller method is expected.

You can have a main template, and set it up something like this:

./application/views/main_template.php
Code:
$this->load->view('header_view');
$this->load->view($main_content);
$this->load->view('footer_view');

So from your controller method, you'd load your view like this:
Code:
$this->load->view('main_template', array(
    'main_content' => 'some_view',
));

I leave it to your imagination what might go into header_view.php and footer_view.php. Smile[/quote]

Oh I see. About that login, it can work. Thanks. Smile

As for the template, I meant something like calling a function which calls the views while needed. I think your idea is more than enough actually. Thanks again.

As for login stuff, I have been looking for advanced login methodology in the Internet, but couldn't find any interesting stuff. What are the things we need to know to make our login system more...best. There has to be something better than the lame "if the query match nothing, then login false" methodology. Form validation and encryption are the only things I can use at the moment. Any idea?
#6

[eluser]TheFuzzy0ne[/eluser]
[quote author="Sky Warden" date="1363705133"]

As for login stuff, I have been looking for advanced login methodology in the Internet, but couldn't find any interesting stuff. What are the things we need to know to make our login system more...best. There has to be something better than the lame "if the query match nothing, then login false" methodology. Form validation and encryption are the only things I can use at the moment. Any idea?[/quote]

I've no idea what you mean by that.
#7

[eluser]Sky Warden[/eluser]
[quote author="TheFuzzy0ne" date="1363705324"][quote author="Sky Warden" date="1363705133"]

As for login stuff, I have been looking for advanced login methodology in the Internet, but couldn't find any interesting stuff. What are the things we need to know to make our login system more...best. There has to be something better than the lame "if the query match nothing, then login false" methodology. Form validation and encryption are the only things I can use at the moment. Any idea?[/quote]

I've no idea what you mean by that.[/quote]
I was talking about a better login system. Just say flow chart for a better login system. At the moment mine is just :
1. Input to the form.
2. Form validation.
3. If valid, check with query.
4. Logged in or not depends on the logical test.
I'm thinking if there's any better method to handle user login. Maybe put some encryption here and there, etc. I really want to push Code Igniter to its limit. Sadly, I have no experience with it. :lol:

How do you usually make login by the way? I think that will help me to figure out a better methodology.
#8

[eluser]TheFuzzy0ne[/eluser]
I think you've pretty much summed it up.

Why do you think there's a better way? The fact that millions of developers have adopted this methodology implies that this is the best way to do it. Why reinvent the wheel? If you want to encrypt anything, then you have your users login over a secure connection.

If you want to push CodeIgniter to it's limits, try implementing namespaces... HA!
#9

[eluser]Sky Warden[/eluser]
[quote author="TheFuzzy0ne" date="1363706434"]I think you've pretty much summed it up.

Why do you think there's a better way? The fact that millions of developers have adopted this methodology implies that this is the best way to do it. Why reinvent the wheel? If you want to encrypt anything, then you have your users login over a secure connection.

If you want to push CodeIgniter to it's limits, try implementing namespaces... HA![/quote]
So...it's the best way already? o_o

I still feel like naked while using that methodology. I think I'm just paranoid. LOL. Yes, I was just wondering if there's any way to make our login system better, without ruining the simplicity.

How can I mark this thread as Solved? I guess the suggestions you told me earlier are enough to answer my questions. As for closing, any link to good logic tutorial? New methodology, system and awesome logic always impress me. People sharing their trick on making features, etc. That will be a good thing to spend time on. Smile
#10

[eluser]TheFuzzy0ne[/eluser]
[quote author="Sky Warden" date="1363707346"]
So...it's the best way already? o_o
[/quote]

I would assume so. Do you have any suggestions to make it better?

[quote author="Sky Warden" date="1363707346"]
I still feel like naked while using that methodology. I think I'm just paranoid. LOL. Yes, I was just wondering if there's any way to make our login system better, without ruining the simplicity.
[/quote]

Perhaps understanding what's being done and why might help that feeling of nekkidness subside? Everything is always open to improvement, and I'm sure there are other ways of doing it, but this is most likely the best compromise. I'm sure you could make it simpler, and in turn sacrifice security. You could also make it more complex, which may gain extra security, but also more complexity. If you're going to do things differently, you need to be able to justify why. If you can't do that, chances are you're going to run into problems, or just give yourself a headache. Smile

[quote author="Sky Warden" date="1363707346"]
How can I mark this thread as Solved? I guess the suggestions you told me earlier are enough to answer my questions. As for closing, any link to good logic tutorial? New methodology, system and awesome logic always impress me. People sharing their trick on making features, etc. That will be a good thing to spend time on. Smile[/quote]

Under each post, there's a button labelled 'Solution'. I wonder what that does...

Maybe when you click it, you instantly get a solution to your problem. Wink




Theme © iAndrew 2016 - Forum software by © MyBB