Codeigniter Model to Controller Flow - php

I get the MVC thing. :) I swear I do.
So, I have the main controller that serves up either the Home 'view' or a Login/Registration 'view' based on if the user is logged in.
Works fine and dandy.
On registration an email link is sent, which the user needs to click to verify the account->email, this happens to be a function in the Home controller that fires off a model is the link is valid.
Now, inside this model is the code to update the database with A: Activated/Not, B: Try Count.
Now, once this is done, I want to display the Login/Register 'view' with an appropriate message (failed, not failed, tried too many times, etc...)
I was going to use a redirect and throw the message type, and message text in a session variable and just display it that way, but then got to thinking that I could bypass that by firing off the Home controller index function passing in an array variable containing the message type, and message text.
Boy was I wrong.
So, how can I do this? I'd really like to stay away from relying on sessions

The respective method of Model should return a value (probably an array) that contains status and count (pseudo-code)
$statusCount = $model->getStatusCount($input);
And later pass the data to View
$view->set('statusCount', $statusCount);
So answering the question: yes, you can evade the $_SESSION.

Related

How to make a multi page registration process in Laravel

I have a concept for how to make a multiple page registration route on my Laravel app, but there is still one area that I can't find. When the user begins to register, the registration route is called. Inside of the register route, multiple views are linked to each other on if statements. For instance, if the user completes one view for the registration, then return the view for the next part of the registration process. The user will continue navigating the routes within the registration process until completed. What I'm trying to find is how to notify the program that the user has completed a view through submitting their information to the app database. Is their a function that allows this to be notified?
Here is a pseudo code format of the route and possible a controller function:
Route posting of register, (possible controller function)
if user completes first section
return next section view
if user completes second section
return next section view
if user completes third section
return next final view
if user completes final section
redirect to different route
end
end
If someone could answer this, I can type the functions and routes for this structure very quickly.
You can use Session to store the values. Let me tell you how.
Route: register/step/1 (use whatever you want)
User Enters The information.
Store it in Session using
session()->put('key','value');.
Redirect to step 2.
Route: register/step/2
User Enters the Information.
Store it in Session using session()->put('key','value');.
Redirect to step n.
.
.
Route: register/step/n
User Enters the Information.
Store it in Session using session()->put('key','value');.
Redirect to step n+1.
Now, At last, You can get data from Session using session()->get('key'); and process it and store it in Database or do whatever you want.
According to me, it's the best way to make multi-page registration using Laravel.
Let me know if this helps! Also let me know if you have any more questions.

Is there any reason to use another function to validate user input on a login screen whent he login screen is on the main page?

As I have right now, I have 3 functions: Main,Login and Panel. Explanation of the 3 functions:
Main
This is the home page. The thing is, on this page there is a login screen as well. So the input will be sent to the function 'Login'.
Login
This is where (only) all the validation will be. If validation fails, I'm sending it back to the Main with the error messages, and if it succeeds, It'll go further to the function 'Panel'.
Panel
Success. You logged in. Nothing special here.
Now my problem is, everything described in the function Login, can mostly fit in the Main function. Is there any reason to not just get everything from Login and put it in Main? So basically when the form is sent, if it fails, stay on the same page but with error messages or 'Succes! you logged in.' I feel like I'm overthinking this too much.
There should be no repetition, validation must be separate function for example validateData(Login) and similar (in calcFunctions.php)
HTML functions should be also in separate functions in for example (view)showFunctions.php then you proceed procedural in index.php you start with if session does not exists you do this else that.
Function login, main and similar are bad because I don't know what they are doing just by looking at their names. Functions you can use for example for particular actions in index.php. showInfoForEveryone, showInfoForLoggedIn, showSingupAndLogin, cleanData, showFormSingUP, showFormSingIn, errortable (array for errors that you can display if there is any), validateData() etc..
This way there is no repetition.

Codeigniter - Pass array from controller, to view, and then to another controller

I have a controller which handles the adding of records to a database. Sometimes these records require a couple of passwords to be entered, to "sign the record". Therefore, when the form is submitted, it is passed to a function in the controller called "getSignatures." This will load a view with inputs for the passwords needed. The users will then enter their passwords to "sign the document". This will then be passed to a function in the controller called "checkSignatures," which will call a function in the model to check the passwords.
However, I still need to pass through or save all the data from when the form is submitted.
I have tried using a global variable to save the form data, but when the checkSignatures function is called, it's called through a new controller. I also tried passing through the form data to the view, saving it in a hidden input and then passing it back through post but you can't save an array in a hidden input.
Any ideas would be great, and sorry for the long-winded question.
Try using sessions as described here. In particular you should look at CodeIgniter's flashdata.
Sessions will be stored on the server for the entirety of the user's browsing session. This will let you access the data again. Flashdata is deleted after the next page load so they don't stick around for long.

Should placeholders be modified from Controllers?

Let's say I have a simple website where users can log in. When they are logged in, I want to show them a different message than users who are not (guests). This message should render in a placeholder, by appending the message to it.
Where should this be done? I was thinking of having my controller check whether the user is logged in or not, and then append to the placeholder via $this->view->placeholder("sidebar")->append()
Why not just put it in the layout itself?
For example, I often have the following situation that affect my layout: if the user is logged in, I want to display his username, a link to view/edit his profile, and a link to logout. If he is not logged in, then I show him a link to login and a link to register.
The code to handle all this uses Zend_Auth::hasIdentity(), Zend_Auth::getIdentity(), and the url() view-helper. To keep the layout code a little leaner, I often push all this into my own view-helper called something like authLinks().
A better solution might be to switch the layout based on the authentication status of the current user. This could be done with a plugin in preDispatch, or in the preDispatch within your controller. By placing the display logic in the view layer, you don't have to update lower level code if you decide to change the message, or remove it all together.
I would personally opt for it being in a controller plugin since it abstracts the concern of checking authentication status and updating the view away from controllers, and prevents you from having to worry about putting the appropriate code in any controllers you create in the future.
That is a matter of personal preference. I always delegate that responsibility to the view, so in my mind yes it should be handled by the view.

design login controller logic using Yii

I am thinking about a login interface. There is a drop down list on that login interface to select one of several user types. The 'log in' button is calling an action to validate the login data in the login controller.
Now, if the login data for user type A is valid, I want to call controllerA. If login data for user type B is valid, I want to call controllerB.
But I know that calling a controller from another controller is not wise.
Then, is there another idea on how to do this login mechanism?
Obviously I can divide the login interface into several parts, each part for each user type.
may this will help you http://www.yiiframework.com/wiki/89/module-based-login/
It seems like in your regular Login action in your Site controller, you could look at the dropdown POST, and with some simple logic validate the user credentials against the right Module/DB tables pretty easily? And just redirect to correct module's after-login url?

Categories