Hello i have a codeigniter project that im trying to get off the ground but im having some serious routing issues.
I followed the codeigniter official tutorial to make the news application but i rather have my news pages static then dynamically on a DB.
problem is i want to organize the pages into separate folders
views/pages = for all the sites basic pages
views/news = all the news posts
on my routes.php file i have this
$route['(:any)'] = 'news/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
i also figured id need a Controller for news so i made this
<?php
class News extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/news/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('news/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
i also included a home.php file in the news folder to see if it works but everytime i try to reach ziplinegolive.com/index.php/news/ I get a 404 error..
Does anyone have any idea how i can simply do this? I have searched ALOT for solutions but no tutorial is like mine and no one explains it simply.
The first problem I noticed is with your routing:
$route['(:any)'] = 'news/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Your first routing entry here has the same key as your second entry. So, your first entry is wiped out. And, the key (:any) will match anything, so your default_controller entry (or any other entry which comes after this one) will never be in use.
Also, I believe that in general, CI suggests that you use prettier URLs like 'news/one' instead of something like 'index.php/news/one'. If you wanted to map 'news/one' to the view method of your news controller, your routing entry could look like this:
$route['news/(:any)'] = 'news/view/$1'
And then your news controller would look something like this
class News extends CI_Controller
{
public function view( $page = 'home' )
{
$this->load->view('news/'.$page, $data);
}
}
Lastly, while I strongly recommend against using file_exists in the manner you prescribe, the defined constant APPPATH is relative, as opposed to an absolute path, which may be causing problems with your file_exists call. I would suggest using an absolute path to make sure there are no path resolution issues when checking for file existence
Related
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.
I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html
i was wondering whats the best way to route to pages in codeigniter? Say for example user wants to route to the index page, but should i create a method in a controller that just fo rthat page, or what is better way?
No need to create separate methods or controllers. Here's how I do it:
class Pages extends CI_Controller {
function _remap($method)
{
is_file(APPPATH.'views/pages/'.$method.'.php') OR show_404();
$this->load->view("pages/$method");
}
}
So the url http://example.com/pages/about would load the view file application/views/pages/about.php. If the file doesn't exist, it shows a 404.
You don't need any special routing to do this, but you can do something like this if you wanted the URL to be http://example.com/about instead:
// Route the "about" page
$route['about'] = "pages/$1";
// Route ALL requests to the static page handler
$route['(:any)'] = "pages/$1";
Routing can be done using the application/config/routes.php file. You can define custom routes redirecting to the index page there. There is absolutly no need to create methods for every page.
A more detailed explanation can be found here: http://codeigniter.com/user_guide/general/routing.html
EDIT:
Didn't realy got what you meant, but here is the solution I use:
class Static_pages extends CI_Controller {
public function show_page($page = 'index')
{
if ( ! file_exists('application/views/static_pages/'.$page.'.php'))
show_404();
$this->load->view('templates/header');
$this->load->view('static_pages/'.$page);
$this->load->view('templates/footer');
}
}
I make 1 controller in application/controllers for the static pages with 1 method in it that I use to load in the static pages.
Then I add this line to application/config/routes.php:
$route['(:any)'] = 'static_pages/show_page/$1';
//you can also change the default_controller to show this static page controller
$route['default_controller'] = 'static_pages/show_page';
In the config file located at /application/config/routes.php
I feel like I have enough experience with CI to finally start fooling around with creating a message board... Or at least thats what I thought, until I got stuck at generating dynamic pages based on subject names (slugs).
So, I creating the controller/model/views and set up a form that submits the necessary info to the database. I pulled the threads out and display them, and generate a link for each one based on the subject title...i essentially followed the CI tutorial, editing it to suit my needs.
However, understanding the concept of generating dynamic page URLs is throwing me off. I understand a lot better by hearing exactly whats happening during the process, and the codeigniter tutorial (news) doesnt explain it well. It simply tells you what to do and how, and not why.
Anyone out there feel up to attempting to explain in greater detail, the process to code dynamic pages.
What I mean by dynamic pages is :
http://your-site.com/news/1/hello-world
http://your-site.com/news/1/foo-bar
where hello world and foo bar.
Here are some parts that confuse me:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
}
}
And heres the routing
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
what does $1 represent? Any specific url that there? When would you use $2? Is it built in code to CI, or can you use any variable?
I'm sure the answer can get more detailed, but If someone could answer some of the above questions, i'm sure it'd be very helpful.
With those questions answered, in theory, what should be done to produce a new page for a forum thread?
Thanks!
what does $1 represent? Any specific url that there? When would you
use $2? Is it built in code to CI, or can you use any variable?
$1 represents a reference to the variable created by the wildcard (:any). There is no $2, because you only have one wildcard.
You would have a second wildcard if you created a route like this:
$route['pages/(:num)/(:any)'] = 'pages/$1/$2';
With that said, the route setup within your question kind of defeats the purpose of CI's MVC architecture and route system, as you're redirecting ALL routes to pages/views, I'm fairly sure you want something like:
$route['default_controller'] = 'pages/view';
$route['pages/view/(:any)'] = 'pages/view/$1';
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
}
}
The corresponding url for this method would be http://example.com/pages/view/. In the method it is set to $page = 'home' because if there is no third segment in the url, it will default to home.
$route['(:any)'] = 'pages/view/$1';
The variable $1 is whatever you have as your (:any). So if you url is http://example.com/testing, it would route to http://example.com/pages/view/testing and that would in turn set your $page var from your view method to "testing".
Using CodeIgniter, I am getting a strange behaviour in my code. What I want is to have a sort of person listing in my database and when I type the id of a given person, a page appears with all the informations we’ve got about this person in the database.
Just a simple thing. I succeeded with the news official tutorial thing and it doesn’t work at all with this !
I wrote a controller inherited class which is named Person, with a viewPersonById method, just like that :
class Person extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('Person_Model');
}
public function index()
{
}
public function viewPersonById($parId){
$data['person'] = $this->Person_Model->get($parId);
$data['title'] = 'Person information';
$this->load->view('templates/header', $data);
$this->load->view('people/view', $data);
$this->load->view('templates/footer');
}
My routes.php is written like this :
$route['people/(:any)'] = 'Person/viewPersonById/$1';
$route['news/create'] = 'news/create';
$route['news/modify/(:any)'] = 'news/modify/$1';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
And all that I get is a 404 when I enter http://127.0.0.1:8888/ci/index.php/people/1 in my browser. What is wrong in my code ? I can’t see it.
Any ideas ? Thanks and sorry for my poor english.
EDIT :
Ok guys, I found out what was wrong. I feel like the biggest jackass ever because the name of my files were weird like "person.controler.php" and "person.model.php". The name of your controller and what you put in routes.php have to match exactly.
So I just had to rename person.controler.php to person.php and person.model.php to person_model.php so the model can be loaded within the controler. CI uses the names of the files to see what it has to load. Be careful with that.
If your controller filename if person.php, then you should change:
$route['people/(:any)'] = 'Person/viewPersonById/$1';
to
$route['people/(:any)'] = 'person/viewPersonById/$1';
lowercase p in person.
PS: Try to open http://127.0.0.1:8888/ci/index.php/Person/viewPersonById/1, you'll know the issue
Sounds to me like your .htaccess isn't set up or being read.
Not all default server configurations will go up the path until they find your index.php script.
Not directly related, but you should have a look at the CodeIgniter Style Guide
Method names should be lowercased and words separated by underscores ('_'). You might not like it and find it a pain in the -you know where-, but trust me sticking with the standards is always the best idea, plus it looks nice and consistent with third party libraries/plugins/helpers.
Plus you don't want to have to custom route every method of every controller. And you also don't want uppercase characters in your urls