Routing in Codeigniter 3 - php

I'm relearning PHP and codeigniter but i can't help but notice how ugly my url looks.
So i have a pages controller: Say lets load homepage.
It would look like this:
public function home(){
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
I guess thats decent as my url will probably look like this:
http://localhost/ProjectName/index.php/pages/home
But whenever i click on a link that involves more data processing for example lets say my home has a list of users and i click on one to view their profile. I ofcourse have to pass something to identify which profile i am to load so my link would look like this:
http://localhost/OnlineStories/index.php/pages/view_userprofile/1
In my controller id have:
public function view_userprofile(){
$userid = $this->uri->segment(3);
//process data then load view
}
I'm not asking for any advanced stuff but i would like to ask simple things to improve my url like correct standard for naming my controller functions.
For example: on previous instances of loading my homepage my function name was view_home now its simply just home so its cleaner.
Maybe i could rename my view_userprofile to simply user_profile.
Anyway is there any better method to passing values from view to controller other than uri segment? its what ive been using so i never gave it much thought. I've seen some use get() but idk if its better than uri segment?
Or should i stick with the uri segment and keep my url as controller/method/parameter?
I haven't really explored post-project polishing in codeigniter so idk if theres a way to polish/change the url(just the name).

Talk about URL in CodeIgniter, you never mention about application/config/routes.php file. Have you seen it ..?
In routes.php file you can create your URL name and mapping it to the controller/function that you have
like this:
$route['user/update'] = 'user_controller/update_data_function'
so, you can call URL http://your.site/index.php/user/update to execute your update_data_function() in your User_controller.php

Related

Codeigniter 2 Controller Fall Through to a different controller

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.
}
}

Unable to access Laravel backend when creating new page?

So basically my site was pre-made so that pages did not exist. The only features that existed was a directory and some posts. No pages. So I had to duplicate the posts function (meaning I replaced all instances of $post and $posts with $page and $pages). It all worked fine. I created a test page which worked. But now I can't access my backend.
This is the URL structure of the pages as seen in the routes file:
Route::get('{url}', 'postsController#viewpage');
So it will basically look like this: www.mywebsite.com/pagehere
But the backend's URL structure looks like this: www.mywebsite.com/admin
So I wonder if my page structure is conflicting with the admin backend?
Whenever I try to access the backend, I get redirected to the homepage.
My controller file has this:
public function adminpage(){
return view ('admin/index')->with("title","Admin");
}
Seems like the rule you put in place matches every route:
Route::get('{url}', 'postsController#viewpage');
You need to specify that the given url must exclude routes tarting with admin, via a regex you can achieve this:
Route::get('{url}', 'postsController#viewpage')->where('url', '[^admin]');
Also I would advise you to use appropriate naming convations: PostsController
Laravel route regex docs

Codeigniter - views in subfolders routes

For organizational purposes I need to have subfolders in my views directory. For example for managing login / registration: views/login_reg/login.php and views/login_reg/register.php.
In my URI want to see: www.mysite.com/login or www.mysite.com/register instead of www.mysite.com/login_reg/login.
I have tried the following in routes:
$route['(:any)/login'] = 'login_reg/login';
Which does not work.
EDIT:
A simple example to elaborate further:
//Controller: login_reg.php
class Login_reg{
function login(){
$this->load->view('login_reg/login');
}
function register(){
$this->load->view('login_reg/register');
}
}
//Routes:
$route['login_reg/(:any)'] = 'login_reg/$1';
So my URI looks like this: www.mysite.com/login_reg/login, or www.mysite.com/login_reg/register
So I want one controller to manage all login / registration related views. But I do not want to see the controller name in the URI. Is this possible? Or is the best approach to have a controller for login, a controller for registration, a controller for password changing etc? This seems a bit excessive. I want to have my files well organised into related directories as this is part of a very large site.
Here you don't need routing at all.
Simply add your folder name while loading view.
For example your controller name for login is like
function login()
{
//code goes here
$this->load->view('login_reg/login');
}
and similar for register.
Hope this helps you.Feel free to let me know if you further queries.
--- Update ---
To hide controller name make chanes in route.php like this;
$route['login'] = 'login_reg/login';
$route['register'] = 'login_reg/register';
and your url will go like this now :
http://yourwebsite.com/login

Codeigniter shorten urls

Is it possible to create such urls in codeigniter?
http://site.com/shorturl/
Where shorturl isn't a physical controller file, but a variable.
I expect the algorythm for parsing url query to be like this:
1) Search for physical controller file. If exists, do standard codeigniter routine. If not
2) Try to load special controller file, where "shorturl" is a variable. Do further stuff inside that controller.
Thanks in advance
The previous answer seems quite good, but thought I'd share what I'd though of.
If you set your 404_override to point to a controller you have set up as follows:
$route['404_override'] = 'welcome/short';
Any URL that doesn't exist (any short URL for example) would get sent there, where you could do the following to check the value:
public function short() {
$shortCode = $this->uri->segment(1);
}
That would give you the value you need to check. If all is well, do the redirect, if the code doesn't exist, you can then use the show_404 method to actually show the 404 page.

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Categories