Simple controller so far:
class Login extends CI_Controller {
public function index()
{
echo 'Hello';
}
}
Route:
$route['(:any)'] = 'login/index';
From my understanding anything typed into the address bar gets routed to the Login controller using the index method? Am I understanding this correctly?
Result: Unable to determine what should be displayed. A default route has not been specified in the routing file.
Related
I'm having some troubles with codeigniter and the MVC model. In my webpage I have a main controller with functions that handles the usual navigation menu with different views home page,about,support... but I have a login view.
I'm not sure if this separation is correct or I should create a controller for each view.
How do I call the login controller, I'm using base_url('main/myView/'); to call the functions inside the main controller, but if I call the login controller from the login view base_url('login/foo'); it does not work.
I'm new to codeigniter and I read their tutorial but I still not sure when should I create a new controller.
Thanks
There are many auth libraries for Codeigniter. I'm sure use the library is better than you write it.
Codeigniter 3.x Authentication Library?
https://github.com/benedmunds/CodeIgniter-Ion-Auth
https://github.com/jenssegers/codeigniter-authentication-library
You don't need to create a multi-controller for each view. you can do it all with one controller.
If you need another controller you can create a new controller and assign it in routes.php. For now, I'm creating 2 controller. Remember controller name start with the capital letter
// pages controller
class Pages extends CI_Controller { // `application/controller/Pages.php`
public function __construct(){
parent::__construct();
}
public function index(){
// default_controller
}
public function about(){
// pages/about
}
public function support(){
// pages/support
}
}
// admin controller
class Admin extends CI_Controller { // `application/controller/Admin.php`
public function __construct(){
parent::__construct();
}
public function login(){
// admin/login
}
public function logout(){
// admin/logout
}
}
Assign url to the controller in application/config/routes.php
$route['default_controller'] = 'pages'; // call lowercase letter
$route['about'] = 'pages/about';
$route['support'] = 'pages/support';
$route['login'] = 'admin/login'; // call lowercase letter
$route['logout'] = 'admin/logout';
If you call base_url('login'), admin controller login function will work
If you call base_url('about'), pages controller about function will work
I am using CakePHP for the first time and I have a trouble with requests.
I have for example this simple controller:
class TestController extends AppController
{
public function index() {
}
public function edit() {
}
public function view() {
}
}
Index is rendering right but when I call /test/edit or /test/view I am redirected (302) to the root of my webpage. Views for each action are created.
It's the same for any other controller in my app.
I cannot figure out why. Do you have any ideas, suggestion what should I check?
Thi was the problem $this->Auth->allow(['edit']); now it is working.
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.
I have some issues with Routes in CI. also new with this framework. I want to ask you guys, (I have read similar example with my problem but it doesn't work for me).
(the link)
Codeigniter routing issues
every time I want to link another page in main page. CI said:![error routes][1]
An Error Was Encountered
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
when i access this url in my browser: /test, i get the error routes.
here is a sample of my routes.php:
$route['default_controller'] = "frontline";
$route['404_override'] = 'error404';
$route['test'] = 'frontline/test';
and this my controller:
class Frontline extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index.php');
}
public function test(){
echo "test";
}
}
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.