codeigniter routing index - php

i have a class called comment and inside i have 3 functions called __construct, index and getComments
Class comment extends CI_Controller
{
public function __construct(){
parent::__construct();
}
public function index($comment_id){
echo $comment_id;
}
public function getComments(){
//do stuff to get comments and print them to screen
}
}
also in my routes folder i have added a new route
$route['comment/(:any)'] = "comment/index/$1";
so when i go to mysite.com/comment/123131313123
it echos the comment id but when i do an ajax call to the getComments() function in the same class it wont work and instead it will show me the word "getComments"
how can i make sure that when i go direct to the index function it will show me the parameter and also be able to do ajax calls without having any other problem with the other functions?
Thanks.

mysite.com/comment/getComments is getting matched to your route
You need to make another route before it which explicitly matches your ajax action
$route['comment/getComments'] = "comment/getComments";
$route['comment/(:any)'] = "comment/index/$1";
Routes are run in the order they are defined.

Related

How to check the condition before reading any method in the Controller in Codeigniter?

How to check a condition before reading all the methods in a controller. After the condition is checked, if it is TRUE, then only the methods in the controller have to be accessible. If the condition returns false then it should be redirected to another controller. How can I achieve this?
Thanks in Advance.
Controller automatically runs construct every time it is initialized, before it runs any other methods, so you can use it to set conditions required to access other controller methods.
Example:
class ExampleController extends CI_Controller {
public function __construct () {
// use construct method of CI_Controller(the parent)
// don't foget this because it wont work without it!
parent::__construct();
if(true) {
//do the thing you want
} else {
// use the url helper to redirect to another page/controller
$this->load->helper('url');
redirect('another-page');
}
}
public function index() {
//display index page...
}
}

Laravel 5 Implicit Route Not Resoving

Editing this post entirely as I realize that I did a poor job of explaining things as they are, and to reflect a few changes already implemented.
The issue: I have an app that has a normal front end, which works perfectly when accessed via app\public. I've added a backend and wish to use a different master layout. I have named the backend Crud. I created Crud\UserController and that has the following:
public function __construct()
{ $this->middleware('auth'); }
public function getIndex() {
return view('crud'); }
In my routes.php file I have the following:
Route::controller('crud', 'Crud\UserController');
I've tried placing that route inside and outside of the middleware group. Neither workds. I do have a file, crud.blade.php, that exists inside resources\views.
The issue is a 404 from apache every time I try to access app/public/crud. Specifically, this error:
The requested URL /app/public/crud was not found on this server.
I'm at a loss as to why the server is unable to find the route to crud.blade.php
ETA: The apache access log just shows a normal 404 when I attempt to access this page. The apache error log shows no errors.
The view() is suppose to point to a view file, that is something like example.blade.php which should contain the content you want displayed when the localhost:app/crud is visited. Then you can do view('example') without the .blade.php.
From your code, change
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('/');
}
}
to
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('crud'); // crud being your view file
}
}
I think you're mixing concepts. Trying to get here will always throw an error:
localhost://app/crud
To enter your app, just try to access:
http://localhost
appending a route you registered in your routes.php file. In your example, It'd be:
http://localhost/crud
If you register
Route::get('/crud', 'Crud\UserController#index')
Then you need an Index method inside UserController, which should return a view (in your example)
class UserController extends Controller{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('crud');
}
}
P.S: about Implicit controllers, you have the docs here.

Codeigniter index function

All Codeigniter controllers seem to start with:
public function index()
{
// stuff
}
Is this a requirement or just good practice? I have an instance where an index may not be needed, for example I have a controller called "Auth" and in it there is a function to register and a function to login - you could argue that the login function is of higher priority but in the interest of naming convention I would rather name my functions. What is best practice here?
It's not a must method. It simply behaves like index.html on apache server.
When there is no html file specified, it automatically goes to index.html.
The same here, when there is no controller method specified index is default.
The index method is simply what's called when the second URL segment is missing. For example:
class Auth extends CI_Controller {
public function index () {
// domain.com/auth
// domain.com/auth/index
}
public function register () {
// domain.com/auth/register
}
}
If you don't need that route, you don't need an index method.

Passing data to the view through the constructor method in Codeigniter

I am currently using the codeigniter tank_auth, at the start of every controller method I have to do the following:
$data['profile'] = $this->tank_auth->get_profile();
The main reason I do this is to display the current logged in username, and also get their privilege level.
I am going over the code trying to go by the DRY principle and have moved a lot of repeated code over to the _constructor method (Like checking if the user is logged in). I am just wondering if there is a way to move this code from the start of every method to the constructor.
My current constructor method looks like so:
public function __construct()
{
parent::__construct();
// If the user isn't logged in redirect to login page.
if (!$this->tank_auth->is_logged_in())
redirect('auth/login');
}
Thanks!
Add variable $data to the controller and use it for all your view data. For example:
public function __construct()
{
parent::__construct();
$this->data['profile'] = $this->tank_auth->get_profile();
}
When calling the view remember to call it like this:
$this->load->view('my_view', $this->data);
You can also extend CI_Controller with MY_Controller and put the login check in the constructor of MY_Controller. Just extend all controllers which need this check from MY_Controller instead of CI_Controller.

codeigniter controller

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

Categories