I'm trying to get my head around custom URIs. I would like my view_county to have the URL:
http://localhost/chipadvisor2/controller_ca/all_county/1
and my view_all to have the URL:
http://localhost/chipadvisor2/controller_ca/all_chippers
If anyone can help me I would greatly appreciate it. The link works for view_county but when I click on the view_all link it shows:
{
"status": false,
"error": "Unknown method"
}
and if I click into view_county first then the view_all link it just keeps adding controller_ca/all_chippers to the URL and doing nothing. Any ideas why?
model_ca
<?php
class Model_ca extends CI_Model {
function __construct() {
parent::__construct();
}
function get_chipper() {
$query = $this->db->get('chipper_reviews');
return $query->result();
}
function get_county($id) {
$this->db->where('location', 'Westmeath');
$query = $this->db->get('chipper_reviews');
return $query->result();
}
}
?>
controller_ca
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Controller_ca extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('model_ca');
}
public function index_get() {
$this->all_chipper_get();
}
public function all_chipper_get() {
$data['chipper'] = $this->model_ca->get_chipper();
$this->load->view('view_all', $data);
}
public function all_county_get() {
$id = $this->uri->rsegment(3);
$data['location'] = $this->model_ca->get_county('location',$id);
$this->load->view('view_county', $data);
}
}
?>
view_county
and
view_all
<html>
<head> </head>
<body>
<div class="container">
View All Chippers
<br/>
View County
<?php echo "<br/><br/>";
foreach ($chipper as $chipperdata) {
echo "<b>Name: </b>".$chipperdata->name." "."<br/>"."<b>County: </b>".$chipperdata->location." "."<br/>"."<b>Description: </b>".$chipperdata->description." "."<br/>"."<br/>";
} ?>
</div>
</body>
</html>
I have added this to the route.php file also but don't know how to put it into effect or how to add the value 'westmeath' to the end. Ideally I would like to pass it as a variable rather than hardcoding it:
$route['default_controller'] = 'Controller_ca';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['all_chipper'] = 'controller_ca/all_chippers';
$route['all_county'] = 'controller_ca/all_county/$1';
Try to give controller method name same as url. Change all_chipper_get() to all_chipper(). Same for other method and check.
Regarding the issue:
and if I click into view_county first then the view_all link it just
keeps adding controller_ca/all_chippers to the URL and doing nothing.
Any ideas why?
Please add base_url() at the starting of URL in anchor tag. It will resolve the append issue in URL. Example:
View All Chippers
<br/>
View County
Also, change your routing as:
$route['controller_ca/all_county'] = 'controller_ca/all_county/$1';
$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';
Related
I have a problem with routing on my website as it's not working at all. At the beginning I wanted to have different Controllers for differetn tabs, but in the end I have changed that to view pages from the Home controller, but even that doesnt work.
Here is my Home controller - index and connection to Model works fine but the travel() function doesnt
<?php
namespace App\Controllers;
use App\Models\HomeMod;
use \Codeigniter\Controller;
class Home extends BaseController {
public function index() {
$this->homeMod = new HomeMod();
$data = array();
$data['total'] = number_format($this->totalCases('total'));
echo view('header');
echo view('welcome_message', $data);
echo view('footer');
}
protected function totalCases(String $columnName) : String{
$result = $this->homeMod->findColumn($columnName);
return count($result) > 0 ? $result[0] : "0";
}
public function travel() {
return view('travel');
}
public function getPostCodeCases() {
if(!IS_AJAX) return;
var_dump("inside");
die();
}
}
I cannot reach the getPostCodeCases() function either (the ajax call in js file works fine, its just not reaching this function).
And my routes file - as you can see i have tries with different variations
$routes->get('/', 'Home::index');
// $routes->get('/getPostCodeCases', 'Home::getPostCodeCases');
$routes->post('/getPostCodeCases', 'Home::getPostCodeCases');
$routes->match(['get', 'post'],'/getPostCodeCases', 'Home::getPostCodeCases');
$routes->get("travel", "Travel::index");
// $routes->add('/travel', 'Travel::index');
// $routes->add('travel', 'Home::travel');
// $routes->add('/travel/(:any)', 'Travel::index');
My base url: https://localhost/CI and $indexPage = '' (in App.php).
I am sure I have missed something, somewhere but not sure where...
Could someone help me to resolve this problem ?
Controller has index() method and display() method.
Method index displays the image. and this method called display() doesn't display the images.
Both methods are in same controller.
Both methods are calling the same view file called
$this->load->view('home/portfolio2');
.
index() method is displaying the image but display() method not displaying the image.
index method code is
public function index() { //$this->load->view('images/homeimage'); //home image.
$this->load->view('home/portfolio2');
$this->load->view('home/header');
$this->load->view('home/viewcategory');
$result['msg'] = $this->W_model->latestupdates();
$result['title'] = 'latest updated';
if($result['msg'] == NULL) {
echo "check for albums in DB";
}else{
$this->load->view('home/latestsongsupdated', $result);
}
$this->load->view('home/footer');
}
display method code is
Public function displayalbums($albums, $lang){
$result['title'] = $lang;
$result['msg'] = $this->W_model->displayalbum($albums);
if($result['msg'] == NULL) { ?>
<h3 style = "margin-left: 20px;">
<?php echo "Songs will be updated soon, Please check for other songs"; ?>
</h3> <?php
} else{
$this->load->view('home/portfolio2');
$this->load->view('home/viewcategory');
$this->load->view('home/albums', $result);
}
}
Any advise, help will be more appreciated.
The correct syntax for image src is:
<?php echo base_url('assets/images/glow_in_worship_by_riyovincent-d571aon.jpg'); ?>
If that does not work echo base_url() somewhere. It should point to localhost/worship if your entire CI is in an htdocs or HTML subfolder that's called workshop. If it doesn't then you should edit your base_url correctly in the config as http://localhost/workship.
You can also use the image helper to generate the image tag (load HTML helper):
<?php echo img('assets/images/glow_in_worship_by_riyovincent-d571aon.jpg'); ?>
Note: this method also requires the base_url to be set correctly.
#raviraj123456
Use the full URL like this: 'http://www.example.com/assets/images/…; in src of the img tag and it will work.
I am working on a basic application using the CI framework.
I have the following error:
404 Page Not Found
The page you requested was not found.
Posted below are my code files.
My Controller code:
class Contact extends CI_Controller{
function _Contact(){
parent::CI_Controller();
}
/*function main(){
$this->load->model('contact_model');
$data = $this->books_model->general();
$this->load->view('books_main',$data);
}*/
function input(){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('contact_model');
if($this->input->post('mysubmit')==true){
$this->contact_model->entry_insert();
}
$data = $this->contact_model->general();
$this->load->view('contact_input',$data);
}
}
Then in Model I have the following code:
class contact_model extends CI_Model{
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
function entry_insert(){
$this->load->database();
$data = array(
'name'=>$this->input->post('title'),
'address'=>$this->input->post('author'),
'year'=>$this->input->post('year'),
);
$this->db->insert('contact',$data);
}
function general(){
$data['base'] = $this->config->item('base_url');
$data['name'] = 'Name';
$data['address'] = 'Address';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['forminput'] = 'Student Registration';
$data['fname'] = array('name'=>'name',
'size'=>30
);
$data['faddress'] = array('name'=>'address',
'size'=>30
);
return $data;
}
}
Finally, my View:
<html>
<head>
</head>
<body>
<div id="header">
<?php $this->load->view('contact_header'); ?>
</div>
<?php echo heading($forminput,3) ?>
<?php echo form_open('books/input'); ?>
<?php echo $name .' : '.
form_input($fname).br(); ?>
<?php echo $address .' : '.
form_input($faddress).br(); ?>
<?php echo $year .' : '.
form_dropdown('year',$years).br(); ?>
<?php echo form_submit('mysubmit','Submit!'); ?>
<?php echo form_close(); ?>
<div id="footer">
<?php $this->load->view('contact_footer'); ?>
</div>
</body>
</html>
Can any one please help me?
Remove this in your Contact Controller:
function _Contact(){
parent::CI_Controller();
}
Replace with this:
function __construct(){
parent::__construct();
}
And in your Contact Model remove this:
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
Replace it with this:
function __construct(){
parent::__construct();
$this->load->helper('url');
}
Hey,
I often had similar Problems. Usually I check the following:
Check the Config file if the correct siteroot is set. I often had live server stuff in there.
Check in the .htaccess if the RewriteBase is set to the correct directory.
to make sure its reading the correct value an echo base_url(); (if this works its usually the .htaccess rewrite base).
Hope it helps.
r n8m
[enter link description here][1]
[1]: http://ellislab.com/forums/viewthread/105880/
hmc
If you have developed your application on Windows, you might have not set first character of Controllers and Models name as a capital character.
like /controllers/home.php to /controllers/Home.php
On Linux file names are case sensitive.
Note:- This is a solution to a possible problem. There may be issues of mis-configuration, server and path variables.
Possible Duplicate: CodeIgniter "The page you requested was not found." error?
I'm having a hard time debugging to locate the issue here.
I've tried echoing $login_by_username and $login_by_email but they aren't echoing.
The problem is when it loads the login form it always puts email as the label.
Controller:
$login_by_username = $this->config->item('login_by_username', 'config1');
$login_by_email = $this->config->item('login_by_email', 'config1');
$this->data['login_by_username'] = $login_by_username;
$this->data['login_by_email'] = $login_by_email;
View:
<?php
if ($login_by_username AND $login_by_email)
{
$login_label = 'Email or Username';
}
else if ($login_by_username)
{
$login_label = 'Username';
}
else
{
$login_label = 'Email';
}
?>
<?php echo form_label($login_label, 'login'); ?>
Config:
$config['login_by_username'] = TRUE;
$config['login_by_email'] = TRUE;
According to our comments.
You have used the autoload.php to call your config file.
You made some tests to make sure that the problem is with your config1.php file and not with CI.
I've made a test and here is what I did.
I have created a file config1.php inside: application/config.
Here it is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['teste'] = 'config1 test';
On my autoload.php also inside application/config:
$autoload['config'] = array('config1');
My Home Controller:
public function index()
{
$data = array('config'=>$this->config->item('teste'));
$this->load->view('welcome_message', $data);
}
My view:
<?php echo $config;?>
And config1 test is echoed as expected.
Make sure that config items login_by_username and login_by_email are there in the config1.php file
Make sure that you are passing $this->data while loading the view eg:
$this->load->view('view.php', $this->data)
I am using Kohana 3.2 and I am having problems calling the ouput of a controller in another controller.
What I want...
In some pages I have got a menu, and in others I don't. I want to use make use of the flexability of the HMVC request system. In the controller of a page I want to call another controller which is responsible for the creation of the menu.
What I have a the moment:
file menu.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Menu extends Controller
{
private $_model = null;
public function __construct(Request $request, Response $response)
{
parent::__construct($request, $response);
$this->_model = Model::factory('menu');
}
public function action_getMenu()
{
$content = array();
$content['menuItems'] = $this->_model->getMenuItems();
// Render and output.
$this->request->response = View::factory('blocks/menu', $content);
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
}
}
somepage.php
public function action_index()
{
$this->template->title = 'someTitle';;
$contentData['pageTitle'] = 'someTitle';
$contentData['contentData'] = 'someData';
#include the menu
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
$this->template->content = View::factory('pages/somePage', $contentData);
$view = $this->response->body($this->template);
$this->response->body($view);
}
If I uncomment the following line in menu.php, I see the menu rendered:
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
So I guess that part is alright. The problem is in the following line in somepage.php:
$menuBlock = Request::factory('menu/getMenu')->execute();
This gives me back a response object. Whatever I do, I do not get the output in $this->template->menu.
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
What must I do to have $this->template->menu contain the view, so I can use it correctly?
I hope this all makes sense. This is the way I would like to do it, but maybe I am completely on the wrong track.
I would do it this way:
class Controller_Menu extends Controller
{
public function action_build()
{
// Load the menu view.
$view = View::factory('navigation/menu');
// Return view as response-
$this->response->body($view->render());
}
}
In your controller get the menu as follows:
// Make request and get response body.
$menu = Request::factory('menu/build')->execute()->body();
// e.g. assign menu to template sidebar.
$this->template->sidebar = Request:.factory('menu/build')->execute()->body();
I would not use the __construct method in your controllers. Use before() instead, this is sufficient for most of the problems (for example auth):
public function before()
{
// Call aprent before, must be done here.
parent::before();
// e.g. heck whether user is logged in.
if ( !Auth::instance()->logged_in() )
{
//Redirect if not logged in or something like this.
}
}
I found the answer to my problem in less than an hour after asking.
I just forgot to put it here.
In somePage.php change :
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
To:
$this->template->menu = Request::factory('menu/getMenuBlock')->execute()->body();
And in menu.php change:
$this->request->response = View::factory('blocks/menu', $content);
To:
$request = View::factory('blocks/menu', $content);
$this->response->body($request);
I hope this will help someone else.