codeigniter flow from controller to view is incorrect? - codeigniter

I have this code:
if(isset($result) && !empty($result))
{
if(is_string($result))
{
show_error('Failed to authenticate user with database');
$this->load->view('error', $result);
}elseif(is_object($result)){
show_error('ALL WENT OKAY!');
$this->load->view('valid', $result);
}
}
Now this code doesn't work as expected. Data is retrieved from database because if I do print_r($result) it will print the content which is correct. But when I have the above it doesn work. it just goes to index() method of my controller and does nothing. Another way I tried was:
$this->load->view('valid', $result);
without any if statements. it goes to authentication page but fails to print $result because it says undefined. any idea would be helpful.
Regards

Do it like this. Pass the result like this.
if($result)
{
$data['result'] = $result;
if(is_string($result))
{
show_error('Failed to authenticate user with database');
$this->load->view('error', $data);
}elseif(is_object($result)){
show_error('ALL WENT OKAY!');
$this->load->view('valid', $data);
}
}else{
$this->load->view('empty_view');
}
This should be suffecient to understand
$data['result'] = $result;
$this->load->view('valid', $data);

Related

How do I setup a second argument for my controller codeIgniter

How do I set up two arguments for my view function in the controller in codeIgniter?
I managed to setup one that takes the category. I need to setup a second one that takes the article alias in the database but when i go to article i get a 404 error page.
In my view function I passed both of them. The first one $category_alias is working but the second one isn't working. It takes me to 404 page.
This is my controller function
public function view($category_alias = NULL, $article_alias = NULL) {
$data['category'] = $this->categories_model->get_all_categories($category_alias);
$data['games_by_category'] = $this->categories_model->get_all_articles_by_category($category_alias);
$data['article_infos'] = $this->categories_model->get_game_infos($article_alias );
if (empty($data['category'])) {
show_404();
}
if (isset($article_alias)) {
$this->load->view('pages/article_display', $data);
}
$data['title'] = $data['category']['category_name']." Games - WalkthroughRemix";
$this->load->view('templates/header', $data);
$this->load->view('templates/top', $data);
$this->load->view('templates/main_menu', $data);
$this->load->view('templates/breadcrumbs', $data);
$this->load->view('pages/category_display', $data);
$this->load->view('templates/footer', $data);
}

Why redirect show blank page in model laravel?

I'd like to ask why the following code works, redirects normally, and data is successfully inserted :
CategoriesController :
public function store()
{
$data = Input::all();
$category = new Term;
if($category->saveCategory($data)){
return Redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
}else{
return Redirect::route('admin_posts_categories')->withError('Failed to add category. #ErrorCode : 13');
}
}
Term model :
public function saveCategory($data){
$this->name = $data['name'];
$this->slug = $data['slug'];
if($this->save()){
$category_taxo = new TermTaxonomy;
$category_taxo->term_id = $this->lastCategoryId();
$category_taxo->taxonomy = 'category';
$category_taxo->description = $data['description'];
if($category_taxo->save()){
return true;
}else{
return false;
}
}else{
return "#Error Code : 4";
}
}
Where as the following only inserts the data but then shows a blank page and doesn't redirect :
CategoriesController :
public function store()
{
$data = Input::all();
$category = new Term;
$category->saveCategory($data);
}
Term Model
public function saveCategory($data){
$this->name = $data['name'];
$this->slug = $data['slug'];
if($this->save()){
$category_taxo = new TermTaxonomy;
$category_taxo->term_id = $this->lastCategoryId();
$category_taxo->taxonomy = 'category';
$category_taxo->description = $data['description'];
if($category_taxo->save()){
return redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
}else{
return redirect::route('admin_posts_categories')->withError('Failed to add category.');
}
}else{
return redirect::route('admin_posts_categories')->withError('#Error Code : 4.');
}
}
Moreover, I'd like to ask a few related questions, does my code conform to correct design patterns, and where should I put the redirect, in the model or in the controller ?
Try this for redirect:
1. return Redirect::back()->withSuccess('Category successfully added.');
OR
2. return Redirect::to(URL::to('admin_posts_categories'))->withSuccess('Category successfully added.');
Add your redirect login inside Controller. Even if you want to put in model (which is not recommended) use Ardent Hook function i.e. afterSave().
First of all never put redirect logic in model. Models are for putting business logic. Second thing check whether you have created route for admin_posts_categories in route.php or not and how you are calling views. If possible post your route code in question.
I recommend not putting redirects in your model. So the first solution would be the best of the two you have.
But back to your problem. It is showing a blank page because your store function is not returning anything. return $category->saveCategory($data); but as previously stated this method is not best practise.
An excellent tip would be to have a look at Laracasts, this will teach you everything you knew, didn't know and more about Laravel.

not able get flashdata in CodeIgniter

Here is my controller code
function index() {
echo $this->session->flashdata('message');
$this->load->view('categoryView');
}
function delete() {
$products = $this->item_model->get_category_id($category_id);
if (count($products)) {
$message = 'Category Name is used by the product. Please change them to another category!';
}
else {
$category_id = $this->product_category_model->delete($category_id);
$message = ($category_id) ? 'Category Deleted Successfully' : 'Failed to Delete Category';
}
$this->session->set_flashdata('message', $message);
redirect('category', 'refresh');
}
After calling the delete function the flashdata has to be set and retrieve that value in index() function of the same controller but I can't.
Am also tried the $this->session->keep_flashdata('message'); before redirect to index function. But still am not get any value in index function.
Also changed $config['sess_expire_on_close'] = FALSE; to $config['sess_expire_on_close'] = TRUE; Still am not get the result.
I am wasted more time(nearly half day). Please anybody help to retrieve the flas data in codeigniter.
There are several probabilities:
1- Check your browser cookie. See if it allows cookies or if any other extension is intervening.
2- Refresh might not send the browser a new request (which thus mean a new URL). Try it for another controller see if it make any change
3- hard code the set_flashdata() with hard-coded value than a variable. set_flashdata("message", "Thank you");
In codeingitor you cannot echo anything in controller.
You have to set in $this->session->flashdata('message'); in view file not in index() function of controller
Try again by putting $this->session->flashdata('message'); in "categoryView" file where you want to display flash message.
You could try:
function set_flashdata_notification($notify_type, $msg, $error_code = null)
{
$ci =& get_instance();
$flashdata_data = set_notification_array($notify_type, $msg, $error_code);
$ci->session->set_flashdata($flashdata_data);
}
function delete() {
$products = $this->item_model->get_category_id($category_id);
if (count($products)) {
set_flashdata_notification('Category Name is used by the product.','Please change them to another category!');
redirect('path_to_view/view','refresh');
}

Calling another controller within a controller if already logged in

I have a login controller, where I do a check wether he is logged in or not like so
if($logged_in){
$this->load->view('profile_view');
}
else{
$this->index();
}
Like you can see I'm loading a view if im not logged in. But instead of loading a view I'd rather have it that I load a controller that loads that view, because I want to send data from the controller to the view.
So I want to do something like this
if($logged_in){
$this->load->controller('profile_controller');
}
and in my profile controller put this
function index(){
$logged_in = $this->logged_in->is_logged_in();
if($logged_in){
$this->load->model('profile_model');
if($query = $this->profile_model->getAllUserInfo()){
$data['records'] = $query;
$this->load->view('profile_view', $data);
}
}
else{
echo "you don't have access on this page";
}
}
But It seems loading a controller from a controller isn't possible! So how can I create my view while sending data to it?
Take a look at how Ben Edmunds authentication library handles it.
if (!$this->ion_auth->logged_in())
{
redirect('auth/login');
}
Ion_Auth Lobrary - http://benedmunds.com/ion_auth/#logged_in

CodeIgniter 404 Bad request error

I'm working with codeIgniter. I am making a login form which checks the members details with the database.
However when I click to login I get this error message:
The requested URL /waitronmain/login.php/login/validate_credentials was not found on this server.
However this is my login.php file and as you can see validate_credentials() does exist.
class Login extends CI_Controller{
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
//load model to query db
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query){ //if credentials validated
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this ->session->set_userdata($data);
redirect('site/members_area');
}
else{ //if not validated load login form again.
$this->index();
}
}
}
Any idea what's going wrong? I should be going on to the 'members_area' page as I know the details I am entering are correct.
Any help would be much appreciated.
what is this??
/waitronmain/login.php/login/validate_credentials
login.php should be not in url i think you should do:
/waitronmain/login/validate_credentials
(assuming you removed index.php from urls and you are inside the waitronmain/ root )

Resources