codeigniter issue with loading view from controller - php

I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)

Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.

CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";

Related

Laravel routes messed up

I'm trying to navigate to a page I created for the logged in profile of a doctor. I have not put in any authentication as I want to take care of the front end first then move to that part of the project. So basically I wanted to navigate to those pages with just putting in the url in the browser but that's not working. I'm new to laravel and am working on a project that was a template first so I'm having a bit of trouble finding things and putting in the correct paths.
I've tried putting the path in the web.php and my PagesController in a few different ways but nothing has worked so far.
my web.php :-
Route::get('/login.profile', 'Frontend\PageController#loginProfile');
my PagesController :-
public function loginProfile(){
$data['page_title'] = 'Profile';
return view('frontend/login.profile');
}
the path to the file :-
\Desktop\doctor\resources\views\frontend\login\profile.blade.php
Try to define the route like this:
Route::get('/login/profile', 'Frontend\PageController#loginProfile'); // I removed the dot from the url
and the controller method like this:
public function loginProfile(){
$data['page_title'] = 'Profile';
return view('frontend.login.profile', $data);
// also view('frontend.login.profile')->withData($data)
// and view('frontend.login.profile')->with(['data' => $data]) should work
// You will have a $data array available in the template
}
the path to the controller should be app/Http/Controllers/Frontend/PageController.php and the path to the view should be resources/views/frontend/login/profile.php.
When pointing to files, many Laravel method replace dots with slashes. That's a feature that's there to allow you to navigate/access stuff in a more "object-oriented" style, i would say. Let me know if it works.

access controller inside the controller directory within another folder

This is my controller directery structure
controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.
-----index
-----Welcome.php
I can access User_reg.php like this:
http://localhost/coin_system/user/user_reg
Now I want to eliminate user from this URL so I added the route
$route['user_reg/(:any)'] = 'user/user_reg/$1';
But it show the error: 404 Page Not Found
I want to access it like this
http://localhost/coin_system/user_reg
How can i access the controller inside the directory in controller?
I have tried to solve using this SO question but it did't help.
I am using Codeigniter latest version. 3.1.5
You have missed function
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['user_reg'] = 'user/user_reg/index';
$route['user_reg/(:any)'] = 'user/user_reg/index/$1';
Or You can have different function
$route['user_reg'] = 'user/user_reg/somefunction'
$route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
Also try with index.php in url
http://localhost/coin_system/index.php/user_reg
When you add route with :any it also find your calling method after controller. But for index(which is default) its not compulsory to all index method So you need to specify route for it also. So just need to add one more route
$route['user_reg'] = 'user/user_reg'; // add this to route file
$route['user_reg/(:any)'] = 'user/user_reg/$1';

Use function in a controller inside a folder that is defined in subfolder Controllers

Ok, guys, this is the case...
I am working in an old porject made it with CodeIgniter v2 (currently all is in localhost). For new features I created a folder called v1 inside the api folder
The structure of the project:
controllers
api
v1
visit.php
orders.php
controller1.php
controller2.php
The problem is that I can not access to the visit.php controller
to test purposes I set the visit controller in the api folder an access it whit this:
localhost/projectname/index.php/api/visit/visits
visits is the function in the visit controller
With this way everything works!! but, when I set the visit controller in the v1 folder I get a 404 page not found error.
localhost/projectname/index.php/api/v1/visit/visits
Extra
Another think that have keep in mind is. This project is using a library to the REST API so, in the visit controller are tho functions
public function visits_get(){
// return an arrays of visits
}
public function visits_post(){
// to add a new visit in a bd
}
So, the function will be called depends on the request method
I have been reading and I found that I have to configure the route.php, actually I did it but without success.
Thanks and I hope you understand what I am asking!
ROUTE.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
This is all that the route.php has in it ._.
To my knowledge, any controllers falling outside the application/controllers/controller_name.php naming convention need to be explicitly defined inside the routes.php file, otherwise CI will not look inside subfolders. It's not much of a problem, actually, add something like this for your controllers:
//You'll need to do this for all of your API controllers, unfortunately
$route['api/v1/(:any)'] = 'api/v1/$1';
//If you have controllers taking arguments, eg. /api/v1/stuff/1
$route['api/v1/(:any)/(:any)'] = 'api/v1/$1/$2';
//Catch-all route for 404's, recommended
$route['api/(:any)'] = 'api/v1/error_api';
Have a look at the routing docs for CodeIgniter 2 for more info.

can't access the controller function(s) in code igniter

hi my folder sturcture is like this
controllers/user/registration/register.php
Inside the register.php controller there is let say for test index function saying 'hello world'.But i cant access the folder index through browser.
My base_url is
$config['base_url'] = 'http://localhost/new/';
But while i write
localhost/new/index.php/user/registration/register/index
I got an error
The page you requested was not found.
what is weird is, I can access the controller fxn of user folder but cant access the controller fxn inside registration folder .And for default controller i have 'home.php'
$route['default_controller'] = "home";
$route['404_override'] = '';
I just want to access the controller/user/registration/register/index fxn which says 'hello world' but it says an error-'The page you requested was not found'.
Thanks
Codeigniter only supports a single level directory structure for Controllers.
Try this link below for Multi Level Subfolder Controller in CodeIgniter :
Multi Level Subfolder Controller in CodeIgniter
Ok after writing some hunch code in my test project ,finally it worked in my case
So here it goes
I follow this link Multi Level Subfolder Controller in CodeIgniter(thanks to K u s h)
http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/
and copy the code and paste in my new/application/core/MY_Router.php as told in that link
and an error came to me like this
Call to undefined method CI_Router::CI_Router() in C:\xampp\htdocs\new\application\core\MY_Router.php
So i changed a little portion of that code to
// Function MY_Router()
// {
// parent::CI_Router();
// }
public function __construct()
{
parent::__construct();
// Your own constructor code
}
And after i was able to access the controllers/user/registration/register.php index fxn
It worked in my case.Thanks to all

CodeIgniter: show a view by typing its url

A newbie here: sorry if the question is too simple, been looking in tutorials but I don't seem to look properly.
I have the
$route['default_controller'] = "mainpage";
in routes.php working properly, and now I just want to view one of the php pages in views folder by typing its url:
http://myserver/folder/thepageIwantToSee
I get a 404 Not Found error. Do I have to use a controller? I just want to see it first before any linking, is there a way to do it?
Thanks a lot!
class yourcontroller extends CI_Controller {
function pageiwanttosee()
{
code
code
$this->load->view('the_view_page', $data);
}
}
Yes, you can write a controller that displays the view specified.
http://myserver/showfile/folder/thepageIwantToSee , where showfile is your controller.
Don't forget to set some security check or disable this on production site.

Categories