So I'm using Codeigniter and I was wondering how I can load in the contents of my view directly into a calling JS file.
I have a view called "form_layout" which contains PHP code to populate form fields.
$('#wizard').smartWizard
({contentURL:'views/form_layout.php?action=1',
transitionEffect:'slideleft',onFinish:onFinishCallback});
but when I do that I get a server 500 error.
Do i have to route through the controller like?
<?php
class Form_manager extends CI_Controller {
public function index()
{
$this->load->view('form_template');
}
}
?>
and do contentURL:Form_manager/index?
Try This
var SITEURL = 'yourdomainname'; //like www.mysite.com
$('#wizard').smartWizard({
contentURL: SITEURL + '/Form_manager/index/action/1',
transitionEffect:'slideleft',onFinish:onFinishCallback
});
<?php
class Form_manager extends CI_Controller {
public function index($action )
{
echo $this->load->view('form_template','',true);
}
}
?>
Yes, you do have to route through a controller. CodeIgniter does not allow you to short-circuit routes to views directly (unlike Laravel). So, you will have to set up the controller as you show and change the url in your ajax call to site_url('form_manager'). You don't need to specify /index since that is the default function that CI calls when none is specified in the URL.
Related
I have defined a Controller in CakePHP 1.3 legacy application running on IIS server windows server 2012, How do I access a view via its controller such that the URL route conforms to application/controller/view.
<?php
class NamesController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Names';
}
you can define a function to access the view within the controller class, e.g.
function view() {
//code for the function
}
complete code with function defined
<?php
class NamesController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Names';
function view() {
//code for the function
}
}
?>
route will now be able to accessed and view request is possible via the route URL application/controller/view
Ensure that associated view is created in the views folder.
URL Rewrite on IIS must be installed, and web.config setup with proper rewrite rules.
I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.
i have one default controller in codeigniter
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
as you know we can call this function
sitename/index.php
or
sitename/
as i have url rewrite in .htaccess
now i want to call same index function with parameter passing like
sitename/seo-services
where seo-services will be paramater passing to index function to load seo-services page.
as default syntax of codeigniter is
example.com/class/function/ID
i want in this formet
example.com/class/ID
where i want to skip the function for default (index) function of class
how can i do this?
Thanks
as you can see i am trying this for seo purpose.
Parameters appended to your url are automatically converted to arguments to your method.
For example if you have a method called index index in your welcome controller, then http://www.example.com/index.php/welcome/index/[parametervalue] will automatically pass [parametervalue] to your method, assuming that you define your function to receive the parameter
class Welcome extends CI_Controller {
public function index($parameterValue=null)
{
//do whatever you need here
//note the default value just in case it is null
}
}
If you want to make things shorter, you will have to define an alias in the config/routes.php file. Also, if you want to get rid of the index.php, you will have to configure your web server accordingly (either through .htaccess if using apache or web.config if using iis)
I have taken over a project written in CodeIgniter, which I have never used before. I have added a new file to views/pages called features.php, and have read on the internet that to make it accessible, I need to create a function in the controller file that will render the page.
I have tried the following:
public function features()
{
$this->render('template', 'pages/features');
}
However, when I try to open features.php, it gives me 404. How can I fix that?
Update 1 - Class
Here is the controller's class code:
class Pages extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('setting_model', 'setting');
$this->load->model('order_model', 'order');
$this->load->model('page_model', 'page');
$this->load->library('form_validation');
$this->load->helper(array('inflector', 'string'));
}
public function index()
{
$settings = $this->setting->get_settings();
$data['document_price'] = $settings->document_price;
$this->render('template', 'pages/index', $data);
}
//This works fine
public function about_us()
{
$this->render('template', 'pages/about_us');
}
//Here is the problem, although it follows the same pattern as about_us()
public function features()
{
$this->render('template', 'pages/features');
}
}
As you are using $this->render I guess you are using the template library. I think you should be using:
public function features()
{
$this->template->set_template('template');
$this->template->write_view('pages/features');
$this->template->render();
}
The php files contained in /views are not directly accessible by typing in some URL. CodeIgniter is an MVC framework. That means that your URLs are mapped to your controllers and the controllers call the views.
What is the name of the class that this function is encapsulated in? Please post the entire class and not just the features() function and we can help you out. If you're working locally, the default mapping to call controllers is: http://localhost/appname/controller/function/param1/param2/etc.
The $this->render() function is not vanilla CodeIgniter syntax, you either inherited a project that is using a templating library, or, there is a sibling render() function inside the controller class.
Check your config/routes.php file as well and consider posting it.
If you want to diagnose the issue, try pinpointing by removing the call to $this->render() and instead using CodeIgniter's native $this->load->view('pages/features') function. If this works, we can be sure it's the library or render() call.
I want to write a new controller file, for example:
aaa.php
class aaa extends CI_Controller
{
public function bbb()
{
// Stuff
}
}
how can i enter aaa.php's bbb(),
The example files are begin with welcome.php's index() function.
how can I change that to begin with my new controller file?
If you provide nothing to the base URL, CI will always assume you want the index action. Like localhost/foo will call foo's index() action. With localhost/foo/bar, you will call foo's bar() action. If you want to call localhost and you want to access foo's index(), you need to check that $route['default_controller'] = 'foo'; is correctly setup in your config.php. (If that's not working, check the .htaccess and the index.php to add it manually)
You want to have a separate function run as the Controller's default function? Why not just call this separate function from index()? Beyond this, I'm not really sure what you're asking...the CodeIgniter user_guide is rather extensive if you haven't looked through it.
If you want to use the bbb function of the aaa controller, you just enter this in the url:
www.mysite.com/aaa/bbb/
As Gsto said, to call bbb function enter the url: mysite.com/aaa/bbb
If you want mysite.com/aaa to call bbb() instead of index() by default, your going to want to create the _remap() function in aaa.php controller to call bbb() instead.
See: CI Controllers - Functions Docs
The way to access your controller's methods in CodeIgniter is by uri. The default routing is:
example.com/controller/function/param1/
So to access aaa's bbb() method, you should access the following uri:
/aaa/bbb
If you want to set aaa's bbb() method as the default page of you application, there is two things to do.
You must first tell CodeIgniter to set aaa as your default controller
/* /application/config/routes.php */
$route['default_controller'] = "aaa";
After that, the aaa's index() method will be call by accessing your base site url. You can't tell CodeIgniter to change de default method index() to something else (without setting some routes), so the easiest way to call bbb() by default would be that:
/* /application/controllers/aaa.php */
class aaa extends CI_Controller
{
public function index()
{
$this->bbb();
}
public function bbb()
{
// Stuff
}
}