I know this question has been asked in a similar fashion several times. However, I'm struggling to find any answers that would work in my situation. I primarily work on Microsoft projects and stepped in on this project to help during crunch.
Here's the situation.
We have a client who has a site with over 600 different pages. In reality each page uses the same template just populates with different data. We've developed a CMS for him which allows him to create new pages at will.
My ideal solution would allow me to store the name of a newly created page in a DB.
Ex. new_page_1 has been created and now exists in the DB. Now when I type in www.mysite.com/new_page_1 this needs to go to a controller that looks up "new_page_1" (in DB) and if it exists loads a view (THIS VIEW WILL NEED TO BE USED FOR ALL 600 pages) which then takes other data from the DB and populates various sections.
So essentially, over 600+ pages need to use the same route array and map to the same controller which then maps to the same view.
I've tried using $route['(:any)'] = 'custom_controler/create/$1 and as well as the same array key but using main and _remap. No matter what every single time it tries to look for the page name in my views (which it will never exist because I'm using one generic view for 600 pages)
Any ideas on how to accomplish this?
UPDATE
routes.php (this is the last line in the file)
$route['(:any)'] = "main/create/$1";
main.php (controller)
class Main extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function create($page)
{
$c = new Category();
$c->get_by_name(ucfirst($page));
$this->load->view('site/index',$c);
}
}
the URL I'm attempting is sitename.servername.com/health sitename and servername obviously substituted.
The error I get is
An Error Was Encountered
Unable to load the requested file: health/main/create.php
Is the error page you're seeing the CodeIgniter error template or a generic server error? That error string sounds very much like you are using Apache or Nginx (or whatever main webserver you use) and its actually not even resolving to your CodeIgniter app at all, but searching for a PHP file that doesn't exist. You'll probably need to use mod_rewrite or something like that to make that URL point at the CodeIgniter install.
Otherwise, your implementation doesn't look completely wrong: you probably need to make sure main.php is the default route as well.
Your application will only look for the page name in your views if you tell it to. Your catch-all controller method should be checking the database for a valid page name, and then regardless of which pages validate, loads the same view (albeit with different data passed to the view).
Using an (:any) catch-all route is perfectly fine. Your controller code somewhere is what's throwing you off. Update your post with the code if you continue to struggle.
Related
I am going to write a super small cms with https://github.com/panique/mini/
Now I want to add a small pages section in the Admin of the site (this can be done easily ).
The advice part comes here:
The url of the mini framework is mostly easy, its /controller/method ( if its the index method then it won't needed to be shown in the url ).
So there is a file which checks if the controller is existing so it can load it.
But the thing is an user is not going to create a controller every time after creating a page.
What would be the best approach to do this ?
This file is checking if that controller exist: https://github.com/panique/mini/blob/master/application/core/application.php
Thanks in advance
The way this handles routing, is much like CodeIgniter. The path would be controller/method/arg1/arg2/arg3..., so you can write a controller, define a method which accepts one (or more) arguments to load user pages.
Assume, user has created a page named news. He/She may load the page via URL pages/view/news.
Basically I'm trying to make the admin section of a website harder to find. I have a Controller named built like this
class Admin extends MY_Controller{ // actions below
I like having the name Admin for the controller since it tells any future developer that might work on the site that this controller contains the admin section of the website.
However; as an extra layer of security, I don't want the admin section reachable by simply typing into a browser www.theDomain.com/admin. I would much rather that any route starting with "admin" return my standard 404 response.
I was hoping this could be achieved in the routes file with something like
$route['admin'] = 'error/404';
I would even accept being able to do this in the constructor of my controller. Thoughts anyone?
You can use different Class name for Admin Controller like "Admin48957". In this case, only who knows this Class Name can access the admin section by typing www.theDomain.com/admin48957. (This is the strategy of many modern CMSs. i.e. prestashop)
If you force return it 404. Then you also could not access by yourself.
But if you still want to return 404. You can use show_404() CodeIgniter function.
Perhaps you could call show_404() in your controller method.
Im not even sure if this is possible but what I am trying to do is this. I store different page modules in a database. When someone goes to a page with one of these modules, the pageController recognizes its a module and ( using the redirect path stored with this module in the DB ) redirects them to a get route which is how I know what info to pull and put on the page.
For instance, if someone was to go to /photo-album the pageController would recognize that was a page module and returns the redirect ( redirect/photo/albums ) and then redirects them to that route.
Route::get('redirect/photo/albums', ['uses' =>'PageController#getPhotoAlbums']);
The problem is the url then becomes /redirect/photo/albums. I would like it to maintain /photo-album
The reason I am doing it this way is because there will be several modules stored and each one will contain different things ( think blog, photo albums, video gallery etc. ). I need the redirect to figure out what goes on that page and what view to show. In this case PageController#getPhotoAlbums goes to the getPhotoAlbums method, pulls the photos and serves up the photoalbums view.
There may be a better way to do this and i'm open to it. Thanks in advance.
I don't have enough rep to comment, so I'll put this as an answer and hope it helps:
You really shouldn't get data from another controller. All your data should be available in the Eloquent model. This is important to the 'MVC' architecture.
There is more discussion on this here: Laravel: Load method in another controller without changing the url
So essentially, when your controller recognises it as a page module it should pull that data from the DB using Eloquent and then pass that to a View. That will mean that the original request URL is still retained.
In Laravel, how would I handle the route if it was generated dynamically? What im trying to do is give the user the ability to create pages on a website so say they wanted to create /about but that isn't listed in the routes file because they would be adding this in through an admin panel. I am trying to figure out how will I make it so I can get the full URL, see if it is a page that already exists in the route file, if it doesn't then check the database to see if that name exists for a page and if it does show the content from that page.
To achieve dynamic routing, you could do something along the lines of:
Route::get('/{pageName}', function($pageName) {
// Do your logic here to determine if the page is in the database, or a file.
});
Take a look at the Laravel documentation on Routing Parameters to see what else you can do with them. In my opinion, the Laravel routing system is very clean and extremely powerful.
Update
One way of doing multiple routes would be like so:
Route::get('/{pageName}/{subPage}', function($pageName, $subPage) {
// Do your logic here to determine if the page is in the database, or a file.
});
You can get into far more advanced URL structures as well by utilizing regular expressions. It's all documented in the link provided above.
I’m a recent user of Codeigniter and am developing a simple backend CMS to manage pages.
Based on a URL (in this example I have hidden “index.php”) : mysite.com/pagename
I would like the system to detect if there is a value of “pagename” in my database, if there is, I need the system to re-route to a custom controller (eg: Pagemaker) and if there is no record called pagename, just do it’s normal thing (i.e. find a controller called pagename)
Currently I have:
$route['(:any)'] = "pagemaker/create/$1";
whereby all requests are forwarded to my custom function.
However I want to change this structure so that if the page does NOT exist in the db, the traditional codeigniter request process is followed.
Can anyone offer any advice about how to complete this? Or any advice about routing custom CMS’s in codeigniter in general?
The best solution is to upgrade to CI 2.0 because it's stable enough and it gives you plenty of useful features.
In your case, set the following route:
$route['404_override'] = 'pagemaker';
If the router doesn't know where to go it just goes to pagemaker controller. This can then check if the first uri segment exists and if not you create a custom 404 page instead of the crappy default one.
And I don't want to hear any of this "Oh but it's not released yet" crap, I've been using it CI 2.0 for almost a year. ;-)
I can think of two possibilities:
1) Edit your custom function to let it redirect your client when page's not in the db
pseudo code:
if($dbresult == null){
redirect("http://yoursite.com/"+$this->uri->segment(3));
}
2) Edit the router class of CI so it will first check if the page's in the db and if not, just continues. This may be somewhat messier as you need a db connection in your Router.php