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";
}
}
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
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.
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.
In the codeigniter I get this message :
Unable to load the requested file: home.php
controller :
cp/
Login
views :
cp/
home.blade.php
class Login extends BaseController {
public function __construct(){
parent::__construct();
}
function index()
{
$this->load->view('home','');
}
}
or :
class Login extends BaseController {
public function __construct(){
parent::__construct();
}
function index()
{
$this->load->view('cp/home','');
}
}
http://www.vitrinsaz1.ir/Mobile/Vitrinsaz/Pannel/cp/login
Hi when loading views remember to make sure that you name it correctly like in your instance you could do the following:
function index()
{
$this->load->view('home_blade');
}
remove the period in the name of the file and replace it with a underscore and then make sure the view file itself is named home_blade.php
Hey there I was also encountered by this error and my error was solved by rechecking the parent folder name try replacing cp.
$this->load->view('cp/home');
with this
$this->load->view('Cp/home');
I think this could be the issue..
My code was working fine on localhost but on server it wasn't loading the view. my error was solved by this.
If the code is correct and you still experiencing that error, change folder permissions :
chmod -R 777 "your folder"
I had a similar problem and that solved it.
Currently my URL is domain.de/site/home, domain.de/site/about.
But i want that my URL ist domain.de/home, domain.de/about etc.
I already have a really simple Controller. In this Controller i`ve a function for each Site:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function about() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
Open application/config/route.php and add following route path
$route['home'] = "site/home";
$route['about'] = "site/about";
You can define the association between a URL pattern and a controller/action in your config/routes.php file. Read the docs here and you will understand how it can be done.
By default, your routes will be {domain}/{controller_name}/{action_name} unless you override that in routes.php where you can assign to your controller/action a different URL pattern.
So probably in your case you would need something along the lines of:
$route['home'] = 'site/index';
$route['about'] = 'site/about';
Even better you might want to set your site controller as default controller:
$route['default_controller'] = "site";
Then all requests starting from the base url will get directed to actions in your site controller. That is {domain}/{action_name} will be directed to site/action_name automatically.