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
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 have a site I am working on that has a URL structure that looks like:
example.com/category-name
This currently goes to a 1000 line long controller called pages that was built years ago and has never been changed. I am inserting a new controller above this in the route that looks like:
$route['(:any)'] = 'new_controller/load/$1
$route['default_controller'] = "pages";
The idea is for new_controller to work like this:
example.com/page-name
This looks up page-name in a database and displays some text made by business people in the admin of the site.
The problem comes from that if page-name is not found in this table, for legacy purposes I must fall through new_controller/load and then use the pages controller normally.
For SEO reasons I can't change the URL structure, it has to remain example.com/category-name and they want it to share the same URL structure example.com/page-name because it's shorter(I tried to convince them to do something like example.com/page/page-name)
From new_controller/load/$1 how would I then call say pages/view/$1 ?
Thanks in advance it is going to really save my butt at work haha
In your route config you can use the config below. new_controller controller handles all page requests so long as their uris don't contain pages/view/.* which pages controller handles.
$route['pages\/view\/(.*)'] = 'pages/view/$1';
$route['(?!pages\/view)(.*)'] = 'new_controller/load/$1';
In new_controller controller - you can do something like the code below so if the slug doesn't exist in database it will be handled by pages controller instead.
public function load($slug)
{
if(/*slug in database*/){
echo "Slug content from database";
}
else { // slug not in database
redirect(site_url('pages/view/'.$slug)); // You can use this
echo file_get_contents(site_url('pages/view/'.$slug)); // or this
$this->load->view('pages_template'); // or this
// or semething else.
}
}
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
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".
I have a site that has a lot of pages that lye at the root (ex. /contact, /about, /home, /faq, /privacy, /tos, etc.). My question is should these all be separate controllers or one controller with many methods (ex. contact, about, index within a main.php controller )?
UPDATE:
I just realized that methods that are within the default controller don't show in the url without the default controller (ie. main/contact wont automatically route to /contact if main is the default controller ). So you would need to go into routes and override each page.
If all of these are just pages, I would recommend putting them into a single controller. I usually end up putting static pages like this into a 'pages' controller and putting in routes for each static page to bypass the '/pages' in my URLs.
If they are share the same functionality, so they should be in the same controller.
for example, if all of them are using the same model to take content from, so, one controller can easily handle it.
Why in one controller? because you always want to reuse your code.
class someController{
function cotact(){
print $this->getContentFromModel(1);
}
function about(){
print $this->getContentFromModel(2);
}
function home(){
print $this->getContentFromModel(3);
}
private function getContentFromModel($id){
return $this->someContentModel->getContentById($id);
}
}
(instead of print, you should use load a view)
See in my example how all of the function are using the same getContentFromModel function to share the same functionality.
but this is one case only, there could be ther cases that my example can be bad for...
in application/config/routes.php
$route['contact'] = "mainController/contact";
$route['about'] = "mainController/about";
$route['home'] = "mainController/home";
$route['faq'] = "mainController/faq";
$route['privacy'] = "mainController/privacy";
and you should add all of these methods within the mainController.php
You can also save the content of the pages in your database, and them query it. For instance, you can send the url as the keyword to identify the page content
$route['contact'] = "mainController/getContent/contact";
$route['about'] = "mainController/getContent/about";
$route['home'] = "mainController/getContent/home";
$route['faq'] = "mainController/getContent/faq";
$route['privacy'] = "mainController/getContent/privacy";
in this case you only have to create one method named "getContent" in the controller "mainController" and this method will look something like this:
class mainController extends CI_Controller
{
public function getContent($param)
{
$query = $this->db->get_where('mytable', array('pageName' => $param));
// then get the result and print it in a view
}
}
Hope this works for you
The page names you listed should probably be different methods inside your main controller. When you have other functionality that is related to another specific entity, like user, you can create another controller for the user entity and have different methods to display the user, update the user, register the user. But its all really a tool for you to organize your application in a way that makes sense for your domain and your domain model.
I've written a blog post about organizing CodeIgniter controller methods that might be helpful to you. Check it out here: http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/