how to ignore id after contrller in CodeIgniter - php

I used Pagination in CodeIgniter.
This is my link in address bar on my localhost
PicTraveller/branches/1.0.1/Implementation/index.php/home
When I click on next then next 5 records will show. After I click on next:
PicTraveller/branches/1.0.1/Implementation/index.php/home/5
I then have to write for this in routes file:
$route['home/(:any)']= 'home/$1';
But this is not work . Please suggest me. Thanks in advance

You can write this without using routes as follow:
Pass parameter to function index on your home conroller in that your code is placed.
Parameter should be nullable as fist no record number is send.
i.e.
class Home extends CI_Controller
{
function __construct() {
parent::__construct();
}
function index($recordId = null) {
if (!empty($recordId)) {
//next button is clicked
} else {
// first time
}
}
}

Related

How to set the Routes and get the GET parameter and value from url in codeigniter

Suppose i have a controller called user and one method myurl like below. The mylink method generate url list from database with pagination support.
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function mylink($id){
/*
Here I want to delete the url(pseudo code :) )
//Check
IF GET request THEN
COLLECT the url ID and DELETE the url
DISPLAY success msg
ELSE
DISPLAY success msg
ENDIF
/*
This function will generate url and pagination
*/
//pagination settings
$config['base_url'].........
......
....
//End of pagination settings (I dont want to write whole text)
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
//call the model function to get the Url data
$data['urllist'] = $this->user_model->GetUrl($config["per_page"], $id);
/*
urllist contain url details and passed to view. where It will create delete url from respective url id like
http://example.com/mylink/del/1
http://example.com/mylink/del/1
http://example.com/mylink/del/1
http://example.com/mylink/del/1
*/
$this->load->view("layout/header");
$this->load->view("mylink",$data);
$this->load->view("layout/footer");
}
}
in my routes i have set
/*
this is for pagination like
http://example.com/mylink/1
http://example.com/mylink/1
http://example.com/mylink/1
*/
$route['mylink'] = 'user/mylink';
$route['mylink/(:num)'] = 'user/mylink/$1';
$route['mylink/del/(:num)'] = 'user/mylink/$1'; //***** Dont understand here how to set*****
But I dont understand how to set the delete url route and make it proper functional. Please help me with this. Thanks

CakePHP not sure if it's the right thing

I have this controller: Resales, and I am in the Administrator controller and need to create a new Resale.. how should I do that?
first option:
Administrator form calls /resales/addResales
second option: Administrator has a method addResale that loads the Resale model and inserts it.
What should I do?
thanks
Administrator controller shouldn't be loading the Resales controller - that should be a model that both controllers use. You should load your Resales model into the Administrator controller in the $uses = array() property at the top of your controller file:
class AdministratorController extends AppController {
var $uses = array('Resale', //the rest of your models);
public function createResale() {
$this->Resale->create();
$this->Resale->set($this->data['Resale']);
$this->Resale->save();
}
}
Other options are that you could use Ajax to post the request for you, or you could use $this->requestAction() in your Administrator controller to use a processing function in your Resales controller:
// administrator controller
public function createResale() {
// define your data here
$result = $this->requestAction('resales/create', array($data_array));
}
Have a look at the manual for more information on requestAction:
http://book.cakephp.org/2.0/en/controllers.html
EDIT
You've just asked about views. In this case, there's no real need to create a view for createResale(), what you might want to do instead is set a Session flash message, then redirect back to your form. You will need to ensure you've included the Session helper at the top of your controller:
class AdministratorController extends AppController {
var $helpers = array('Session', // any others here);
Then you prevent the layout and render of views, do your thing and set a session flash with the results message:
public function createResale() {
// don't render a view or layout
$this->layout = '';
$this->render(false);
// process your request
$result = // do stuff... return true or false for result
$msg = $result ? 'Added successfully!' : 'Error adding resale!';
// set flash message
$this->Session->setFlash($msg);
// return to that form
$this->redirect(array('action' => 'formYouCameFrom'));
}
Now on your form, you'll simply do this:
echo $this->Session->flash();
... which will output the results.

ignoring frontpage.php in codeigniter

I'm currently working on an existing website created in codeigniter.
Whenever a user enters a page, he gets redirected to frontpage.php, that checks if the user is logged in, if not he gets redirected to the login page.
Now, I have one page where this frontpage.php shouldnt be executed, and any user can enter it.
Any help is greatly appreciated.
Had a similar problem and solved it this way by using some online tutorials
1: Make a seperate loginpage (ex login.php) prior to the 'frontpage.php'.
2: Pass the login, password and a session variable to the frontpage.
3: Recode you 'frontpage.php' to check for the session variable passed by 'login.php'.
If u entered the page trough the normal way it will use the normal login.
if u entered the page trough the new 'login.php' page it will be picked up by the recoded 'frontpage.php' and bypass the normal way.
Hope this helps
Grtz
Re-route everything to your pages controller and use this as your default
$route['default_controller'] = 'pages';
$route['(.*)'] = 'pages/index/$1';
-
class Pages extends CI_Controller
{
protected $currentUser = null;
public function __construct()
{
parent::__construct();
$this->currentUser = Auth::getCurrentUserObject(); //check user is logged in
}
public function index($uri='home')
{
$sizeOfSegments = sizeof($this->uri->rsegments);
if ($sizeOfSegments >= 3)
{
$uri = $this->uri->rsegments[3];
}
else
{
$uri = 'home';
}
$pageFound = Page::find($uri); //query the database
if (!$pageFound)
{
return show_404($uri); // find out where there were headed
}
unset($sizeOfSegments, $uri);
if(is_null($this->currentUser) OR !$this->currentUserHasPermissionToViewThisPage OR !$pageIsNotPublic)
{
return redirect('login');
}
$this->load->view();
}
}
make new controller
class Newpage extends CI_Controller {
public function index(){
$this->load->view('newpage');
}
}
now goto
yourhost.com/index.php/newpage
May be your default Controller have a coding of checking whether user is logged or not. Please remove or change the code in controller.
I think you need to delete the controller, it works like that!

Change url in php after reloading a page

My home page url looks like this
http://localhost/mediabox/home/box/12
I have a link of language on home page when a user clicks on that link i am sending that language id as query string , the page reloads and the url converts to
http://localhost/mediabox/home/box/?bid=12&ln=2
I want to reload the page with the new language but don't want to change my url i-e I want my URL to be
http://localhost/mediabox/home/box/12
after the page loads
How it is possible please me some gud ideas
Thanks
VIEW
<a href=<?php echo site_url('home?language=indonesian');?>>Indonesian language</a>
CONTROLLER
class Home extends CI_Controller {
public function index()
{
$language = $this->input->get('language');
if($language){
// Put your code here
// Now u can set session
$this->session->set_userdata('language', $language);
redirect('home');
}
if($this->session->userdata('language'))
{
var_dump($this->session->userdata('language'));
}
echo 'Hello World!';
}
}

SEO url gives 404-error in CodeIgniter

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
}
}
}
?>

Categories