Welcome Guest, Not a member yet? Register   Sign In
Load view from Controller issue
#1

Hi there,

I am new to CodeIgniter framework and needs someone help, please.
I have an issue to redirect user back to Contact Us form after its submission.

My default controller is defined as below.

PHP Code:
<?php

class Pages extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
   }

 
   public function view($page 'home') {
 
       if (!file_exists(APPPATH 'views/pages/' $page '.php')) {
 
           show_404();
 
       }

 
       $data['title'] = ucfirst($page);

 
       $this->load->view('templates/header');
 
       $this->load->view('pages/' $page$data);
 
       $this->load->view('templates/footer');
 
   }



Now my view pages are in the sub-directory called pages, for example /views/pages/contact-us.php. So when I access this page like https://www.mysite.com/contact-us then it works fine and loads the contact us form as expected.

Look at my .htaccess file that removes index.php from the URL.

Code:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png|resources|robots\.txt)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

And also look at my routes.php file.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

$route['sendemail'] = 'sendemail/index';

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

Now after user submits the contact us form it goes to controller called SendEmail.php and then I want user to come back to Contact Us form with message either successful or failed. I am getting an email to my gmail account but when it comes to loading VIEW it does not work.

Look at code of SendEmail.php

PHP Code:
<?php

defined
('BASEPATH') OR exit('No direct script access allowed');

class 
SendEmail extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
   }

 
   public function index() {
 
       // Check form submit or not
 
       //var_dump($this->input->post());

 
       if ($this->input->post('submit') != NULL) {

 
           // POST data
 
           $postData $this->input->post();

 
           //SMTP related configuration is defined in /config/email.php
 
           //it loads automatically by CodIgniter
 
           
            $this
->load->library('email');
 
           $this->email->set_newline("\r\n");

 
           $this->email->from($postData['email'], $postData['name']);
 
           $this->email->to(ADMIN_CURRENT_EMAIL);

 
           $this->email->subject(CONTACT_US_FORM_SUBJECT);
 
           $this->email->message('Testing the email class.');

 
           $data['is_error'] = false;

 
           if ($this->email->send()) {
 
               $data['message'] = 'The email has been sent successfully.';
 
           } else {
 
               $data['is_error'] = true;
 
               $data['message'] = 'Sorry, the email was not sent successfully. Try again later or send a direct email to ' ADMIN_CURRENT_EMAIL;
 
               //echo $this->email->print_debugger();
 
           }
 
           
            $this
->load->view('contact-us'$data);           
            
        
} else {
 
           echo "Invalid request.";
 
           die();
 
       }
 
   }

}

?>


I am getting below error. As I said above I manage to get email to my gmail account but loading a view does not work.

An Error Was Encountered
Unable to load the requested file: contact-us.php

Can you please help me?

Thanks - Hitesh
Reply
#2

Is the file contact-us.php located in the /application/views/ folder?
Reply
#3

Yes Dave...its in the /application/views/pages folder.

Thanks.
Reply
#4

(06-11-2018, 01:34 PM)hiteshpatel1979 Wrote: Yes Dave...its in the /application/views/pages folder.

Thanks.

If the path to the file is  /application/views/pages/contact-us.php

Then this line

PHP Code:
$this->load->view('contact-us'$data); 

should be

PHP Code:
$this->load->view('pages/contact-us'$data); 
Reply
#5

(06-11-2018, 03:20 PM)dave friend Wrote:
(06-11-2018, 01:34 PM)hiteshpatel1979 Wrote: Yes Dave...its in the /application/views/pages folder.

Thanks.

If the path to the file is  /application/views/pages/contact-us.php

Then this line

PHP Code:
$this->load->view('contact-us'$data); 

should be

PHP Code:
$this->load->view('pages/contact-us'$data); 
Thanks Dave.
I will try today and let you know how it goes.
Reply
#6

(This post was last modified: 06-12-2018, 03:13 AM by InsiteFX.)

Your controller is pages/view/$1 because if you look you have no default index method.

So you need to access it like this.

PHP Code:
// wont work
$this->load->view('contact-us'$data);

// should be like this.
$this->load->view('pages/view/contact-us'$data); 

If that wont work you can try this method below.
PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('redirectBack'))
{
    
/**
     * redirectBack ()
     * -------------------------------------------------------------------
     *
     * Redirect back to previous page
     *
     */
    
function redirectBack()
    {
        if (isset(
$_SERVER['HTTP_REFERER']))
        {
            
header('Location: '.$_SERVER['HTTP_REFERER']);
        }
        else
        {
            
header('Location: http://'.$_SERVER['SERVER_NAME']);
        }

        exit;
    }


Place it into one of your CodeIgniter helper files.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Hi Dave,

In fact I had to put as below in the controller while redirecting back to contact us form.

PHP Code:
/*1*/ $this->load->view('templates/header');
/*2*/ $this->load->view('pages/contact-us'$data);
/*3*/ $this->load->view('templates/footer'); 

If I only add line#2 then contact us form does not include header and footer views in the final rendering of the view. So I had to do it that way. I find it annoying. Is there any better way so I will not have to include header and footer templates while loading view from the controller?

With above code I manage to load contact-us view but then in the address bar it does not show contact-us (http://localhost:8080/42patidarsamaj-staging/contact-us) rather it shows controller name (http://localhost:8080/42patidarsamaj-staging/sendemail). 

Please advise.

Thanks.
Reply
#8

Hi there,


Thanks for taking off time to post your reply.
I tried as you said but it did not work.

PHP Code:
$this->load->view('pages/view/contact-us'$data); 

At the moment in /application/helpers folder there is not any file. Do you mean I need to create a new PHP file and place the redirect code there? If yes, then will be a PHP class file or regular PHP template?

Kindly advise.
Reply
#9

(This post was last modified: 06-12-2018, 03:44 AM by InsiteFX.)

PHP Code:
// Try changing this
$route['(:any)'] = 'pages/view/$1';

// To this
$route['pages/view/(.+)'] = 'pages/view/$1'
PHP Code:
$route['translate_uri_dashes'] = FALSE;
As 
evident by the boolean valuethis is not exactly a routeThis option enables you to automatically
replace dashes 
(-with underscores in the controller and method URI segmentsthus saving you additional
route entries 
if you need to do thatThis is requiredbecause the dash isn’t a valid class or method name
character 
and would cause a fatal error if you try to use it
I would also change contact-us to contact_us I never use hyphens in file names.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

Hi there,


After I made a change in routes.php as you suggested now my hyperlinks/direct URLs for all the views stopped working.
For example:

Code:
http://localhost:8080/42patidarsamaj-staging/directory
http://localhost:8080/42patidarsamaj-staging/about-us
etc...

Sorry but now I am putting back what was before....I did not think it was this tough to load VIEW from Controller.

Please help if you can.

Thanks.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB