Load view from Controller issue - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Load view from Controller issue (/showthread.php?tid=70866) |
Load view from Controller issue - hiteshpatel1979 - 06-11-2018 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 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 And also look at my routes.php file. PHP Code: <?php 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 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 RE: Load view from Controller issue - dave friend - 06-11-2018 Is the file contact-us.php located in the /application/views/ folder? RE: Load view from Controller issue - hiteshpatel1979 - 06-11-2018 Yes Dave...its in the /application/views/pages folder. Thanks. RE: Load view from Controller issue - dave friend - 06-11-2018 (06-11-2018, 01:34 PM)hiteshpatel1979 Wrote: Yes Dave...its in the /application/views/pages folder. 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); RE: Load view from Controller issue - hiteshpatel1979 - 06-11-2018 (06-11-2018, 03:20 PM)dave friend Wrote:Thanks Dave.(06-11-2018, 01:34 PM)hiteshpatel1979 Wrote: Yes Dave...its in the /application/views/pages folder. I will try today and let you know how it goes. RE: Load view from Controller issue - InsiteFX - 06-12-2018 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 If that wont work you can try this method below. PHP Code: // ----------------------------------------------------------------------- Place it into one of your CodeIgniter helper files. RE: Load view from Controller issue - hiteshpatel1979 - 06-12-2018 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'); 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. RE: Load view from Controller issue - hiteshpatel1979 - 06-12-2018 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. RE: Load view from Controller issue - InsiteFX - 06-12-2018 PHP Code: // Try changing this PHP Code: $route['translate_uri_dashes'] = FALSE; RE: Load view from Controller issue - hiteshpatel1979 - 06-12-2018 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 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. |