I'm trying to mix static and dynamic content in a CodeIgniter 3.1 tailored website. I'm using the tutorial example given for the static content:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
I'm afraid this would be quite messy for the purpose, since (:any) is too generic and I don't want to use something like "/static/(:any)" route.
Any suggestions on how to achieve a solution that let me have a static and controller named friendly URL?
Every idea is welcome and very much appreciated.
As my question seems to be difficult to understand, I'll try to ask it once again:
Is there a way to combine static content with the above code (from codeigniter tutorial) and the usual approach http://example.com/controller/index_named_method dynamic content handling?
Can you give an example?
Should I change $route['(:any)'] for every static webpage's name i.e.:
$route['(home|contact|links)'] ?
Thanks in advance
Well, I must say that I got to an answer myself; as said in my previous post editing, mixing static and dynamic content using index() method in your Controller taking advantage of the tutorial example can be accomplished adding a route with all your controllers name like this:
$route['(books|flowers|links)'] = '$1';
Thanks to those who helped.
Related
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
Just got a question about codeigniters routing when you only want the first segment to be valid. Cant seem to find a good answer when googling.
So I have a basic route for my general pages:
$route['(:any)'] = 'common/pages/view/$1';
Pages is the class and view is the method along with the name of the page as a variable (pretty much like the example on the ci manual).
This works fine when I go to:
www.mysite.com/mypage/
However when I then go to:
www.mysite.com/mypage/randomstring/
This also loads mypage which is essentially a duplicate.
Is their a way to tell the any route to only apply to the first segment and if more exist do a 404?
If worse comes to worse I will just add a check in the method to see if the 2nd segment exists, if so show_404 but just curious as to if it can be done purely in the routes.
Thanks for reading and I hope that makes sense.
You could just use Regex instead?
$route['([^/]+)'] = 'common/pages/view/$1';
This would prevent the URL after your domain from containing / and if it does, it will call the default 404 page.
I haven't tested this but it "should" work ;)
Try this:
$route['(:any)/(:any)'] = "none_existent_controller";
$route['(:any)'] = "common/pages/view/$1";
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 need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
How to use this things, the routes ?
For example, i my page is
controller/function
and i want the controller part and the function part to become
name
$route['controller/function'] =
"name";
and i tryed like that, but nothing happens, still when trying to go to controller/function it is controller/function not name
you need to do it the other way round.
actually you define a route to a desired controller function:
$route['en/name'] = 'en_controller/name_function';
also see here: CI Routing
You should read the manual URI Routing in CI URI Routing. There are some examples that will help you understand how to make your routing to work.