I have started working with Codeigniter. So, I have two controller Tasks and Project. In my Task Controller I have a function to edit an existing task and after the task is edited I want to redirect to the Projects controller's display method which takes a parameter as the project_id associated with the task.
Here is my task controller:
public function edit($task_id,$task_project_id){
$this->form_validation->set_rules('task_name', 'Task Name','trim|required');
$this->form_validation->set_rules('task_description', 'Task Description','trim|required');
if ($this->form_validation->run() == FALSE){
$result=$this->task_model->get_task($task_id);
$data['task_name']=$result->task_name;
$data['due_on']=$result->due_on;
$data['task_description']=$result->task_description;
$data['id']=$task_id;
$data['task_project_id']=$task_project_id;
$data['main_view']="tasks/edit_task";
$this->load->view('layout/user', $data);
} else {
$data['task_project_id']=$task_project_id;
$data['task_name']=$this->input->post('task_name');
$data['due_on']=$this->input->post('due_on');
$data['task_description']=$this->input->post('task_description');
$data['id']=$task_id;
if ($this->task_model->edit($data)){
$this->session->set_flashdata('task_edited' ,'Task Edited Succesfully');
redirect(projects/display/$task_project_id); THIS SEEMS TO BE THE PROBLEM .
}else{
$this->session->set_flashdata('task_edited_failure' ,'Task Not Edited Succesfully');
redirect(projects/edit);
}
}
}
Here is my Projects Controller with its index and display function :
public function index()
{
$data['projects']=$this->projects_model->get_projects();
$data['main_view']="projects/home_view";
$this->load->view('layout/user.php',$data);
}
public function display($project_id){
$data['task']=$this->task_model->get_tasks($project_id);
$data['project']=$this->projects_model->get_project($project_id);
$data['main_view']="projects/display";
$this->load->view('layout/user.php',$data);
}
So, in my task controller I am trying to redirect to the projects controller's display method using redirect(projects/display/$task_project_id) as the Projects Controller's display function is display($project_id). But this is redirecting to me the Project controller's index function. What is wrong with the redirect ? I thought the URL structure in CI was controller/function/parameter1/parameter2.
You made mistake in writing redirect function. It should be like as below.
redirect('projects/display/'.$task_project_id);
redirect('projects/edit');
redirect($uri = '', $method = 'auto', $code = NULL)
redirect() function in codeigniter uses the url you define in your root. You can't directly jump to controllers.
Related
I can have the following urls
www.example.com/accounts/
www.example.com/accounts/signup
www.example.com/accounts/signup/validate
www.example.com/accounts/login
for each case, accounts becomes my controller, and index, signup, signup and login becomes my actions (or methods) respectively. I have to render different views based on what my actions are. Here is an example of what my code looks like
index
$url_segments = explode('/', $url);
$controller = !empty($url_segments[0]) ? $url_segments[0] : 'home';
array_shift($url_segments); // removes the controller name from array
$action = isset($url_segments[0]) && !empty($url_segments[0]) ? $url_segments[0] : 'index';
array_shift($url_segments); // removes the action name from array
$controller = ucfirst($controller);
$controller = new $controller($url_segments);
$controller->$action();
controller class
class Accounts{
private $url_segments;
public function __construct() {
$this->url_segments = $url_segments;
}
public function index() {
// index code here
}
public function login() {
// login code here
}
public function signup() {
if (!isset($this->url_segments[0])) {
// url entered was: example.com/signup
} else if (isset($this->url_segments[0]) && $this->url_segments[0] == 'validate') {
// url entered was: example.com/signup/validate
}
}
}
from how my code appeared above, it can be seen that as parameters keep adding after the controller and action part of the url I'll need to keep using conditional statements to run the proper code as in the case of /signup/ and signup/validate. Is this method of using conditional statement to load view based on parameters efficient or is there a better way of doing this.
I would recommend you to make use of a routing system like Symfony Routing. There you could add a new Route for every url and redirect them to your specific controller.
Example Route:
$routes = new RouteCollection();
$routes->add('/accounts_signup', route('POST', "/accounts/signup", 'App\Controller\AccountController:signup'));
return $routes;
This route would call the signup method in the AccountController calss when www.example.com/accounts/signup get called with a post request.
I recommend you to use something like this. Even if this might be a bit complicated for the beginning, after reading (and understanding) the docs this will safe you a lot of time and it will make your code more readable as well.
I wants to make a list page and add page
for list page my route function is
Route::get('admin/auctionlist','AdminController#showAuctionList');
and controller is
public function showAuctionList(){
$auctions = DB::table('auctionitems')
->leftjoin('campaigns','campaigns.id','=','auctionitems.campId')
->select('auctionitems.*','campaigns.title')
->get();
return View::make('admin/auctionlist')->with('auction',$auctions);
}
it works fine and my url is
http://localhost/vishal/site/public/admin/auctionlist
And for my add page route is
Route::post('addAuction',function(){
$obj = new AdminController() ;
return $obj->addAuction();
});
controller is
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return $this->showAuctionList();
}
It redirects to list page but url showing as
http://localhost/vishal/site/public/addAuction
Aucually i want to the url as
http://localhost/vishal/site/public/admin/auctionlist
how can i get it.?
Finally i solved the issue.
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return Redirect::to('admin/auctionlist');
}
its in laravel 4.
in laravel 5
it should be
return $this->redirect()->route('some-route-name');
Just use return redirect. And why messing your routing with functions? Just rule it to use the function in your controller:
routing
Route::post('admin/auctionlist','AdminController#addAuction');
controller
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return redirect('admin/auctionlist');
}
I have a url www.mywebsite.com/store/123456
where store is my controller class and I have a index function in it where Im getting the value after after store ,ie 123456.But im not able to achieve it.
As found online,I have tried adding this to routes $route['store/(:any)'] = 'store/index'; also tried
$route['store/(:any)'] = 'store/index/$1';
but doesn't seem to work.Please help me out.
In controller index function is
public function index()
{
echo $this->uri->segment(1);
}
But its not working.Pleae help
You are invoking 123456() method instead of index() method therefore you get CI's 404.
The simplest way is to use this kind of route
$route['store/(:any)'] = 'store/index/$1';
AND in top of it add parameter to index function in your case
public function index($parameter)
{
echo $this->uri->segment(2);
}
note that I changed segment parameter please see documentation.
using _remap()
function _remap($parameter){
$this->index($parameter);
}
function index($p)
{
echo $p; //shows 123456
}
If I remember correctly:
segment(n) gives you the segments of your uri before the routing takes place
rsegment(n) gives you the segments of your uri after routing (if routing occurred or the same as segment if it didn't)
so for /store/123456 getting rerouted to /store/index/123456
segment(1) == rsegment(1) == 'store'
rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'
Route:
$route['terms_and_conditions'] = 'SO_front/page/1';
controller :
public function page($id)
{
echo $id;
}
Output :
1
Here my solution for CI3...i would prefer laravel for advanced routing, orm,restapi, build in vuejs.
$route['store/(:any)'] = 'store/index/';
public function your index(){
$parameter=$this->uri->segment(2);
//use parameter for sql query.
}
Let say your route $route[store/product/update/(:any)] , then $parameter=$this->uri->segment(4)
Problem?. You will need to change entire file code if you plan to change the route name include view, controller, and custom route.
I'm using codeigniter I have this BuildTemplate function in my [HOME] controller that authenticates a user. I have another controller called [AJAXCONT] that has a function called search() that returns data to populate my search table in the view. I would like to retrieve that controller's data from it's search function right after I generate the $top_bar_data view. How can I code this. Is my approach correct here?
private function BuildTemplate($view, $data) {
if($this->session->userdata('logged_in_faculty'))
{
$session_data = $this->session->userdata('logged_in_faculty');
$top_bar_data['Firstname'] = $session_data['Firstname'];
$top_bar_data['Lastname'] = $session_data['Lastname'];
$master_data['f_top_bar'] = $this->load->view('f_top_bar', $top_bar_data, true);
//I WANT TO RETURN ALL DATA IN THE AjaxCont controller returned by the search function HERE //
//$search_data = (all data from my search function in AJAXCONT controller )
// $master_data['search_results'] = $this->load->view('search_results', $search_data, true);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
return $this->load->view('master', $master_data, true);
}
You should move all your functions to models, controllers should only retrieve the data from the models and output it by loading views. CodeIgniter's controllers are only called by URI/Routing, you cannot call a controller or controllers methods from anywhere else.
Create a model to work with users, a second model that does what your search does. Then, from controller call those in the order you wish.
I am pretty new to codeigniter. I do know php.
How can I accomplish to load the right view?
My url: /blog/this-is-my-title
I’ve told the controller something like
if end($this->uri->segment_array()) does exist in DB then load this data into some view.
I am getting an 404-error everytime I access /blog/whatever
What am i seeing wrong?
unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.
A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title
A route like:
$route['blog/(:any)'] = "blog/view/$1";
may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'
The may be more possibilities:
The most common - mod_rewrite is not active
.htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
The controller does not exist or is placed in the wrong folder
Suggestion: if you only need to change data use another view in the same controller
if (something)
{
$this->load->view('whatever');
}
else
{
$this->load->view('somethingelse');
}
If none of those works post a sample of code and configuration of .htaccess and I'll take a look.
The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.
No routing required!
enter code here
<?php
class Blog extends Controller {
function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->show_post();
}
}
function index()
{
// show blog front page
echo 'blog';
}
function edit()
{
// edit blog entry
}
function category()
{
// list entries for this category
}
function show_post()
{
$url_title = $this->uri->segment(2);
// get the post by the url_title
if(NO RESULTS)
{
show_404();
}
else
{
// show post
}
}
}
?>