Passing get variables to index page in codeigniter - codeigniter

I am creating a social networking web application using codeigniter. The web application has features similar to facebook pages. I need to accomplish something which seems to be impossible. Since codeigniter recognizes controllers and functions based on the URL order, I am unable to directly pass an argument to the controller index function. I am trying to access a page as "www.mywebsite.com/page/CodeIgniter" where 'CodeIgniter' will be the page name which the 'page' controller will accept it as an argument and accordingly parse the page. Since codeigniter parses url as "www.mywebsite.com/{controller}/{function}", I am unable to link to a page as how we do in facebook. Could anyone help me out on how to overcome this issue.

Using routes should solve this issue;
config/routes.php
$route['page/(\w+)'] = 'page/index/$1';
This example is simple, but do note that it will "hijack" most other calls which have only 2 segments.
If you got a few values which you will pass, do consider something such as:
$route['page/(CodeIgniter|Drupal|Joomla)'] = 'page/index/$1';
Or a preffix:
$route['page/cms_(\w+)'] = 'page/index/$1';
And have the url domain.com/page/cms_CodeIgniter instead.

Related

Is it possible to extend Yii URL routes and rules through a Controller?

Is it possible to extend Yii URL routes and rules through a Controller on the fly, or by extending the CController class itself?
I am really stuck with this, have not tried anything.
How to do this in/with Yii?
First you should be aware of what URL rules are used for:
Parsing an incoming URL. The urlManager component resolves a URL to a route so that Yii can call the right action in a specific controller and module.
Creating URLs from calls to createUrl() in your code.
Considering (1) above it is obvious, that you can not add URL rules in your controller if you want to use these URLs in your application. It's much too late, as Yii already has gone through the process of resolving a request to a route. Even if you'd only wanted to create URLs it doesn't make much sense as your application will never understand them.
The correct way to bring more dynamics into your URL parsing/creation is to use a custom URL rule class. In there you can write any code you want to create and parse even the most complex URLs. The topic is described in the manual.

ASP.NET MVC 3 Redirect/route URL back to subdomain

I have a site on a subdomain (www.website.com/foobar) which pulls the default page view. Due to the nature of MVC apps, this default page view can also be reached through the "controller/action" URL of the default content (www.website.com/foobar/{controller}) and (www.website.com/foobar/{controller}/{action}).
For the sake of analytics and tracking, I would like any of these other URLs, besides the original short (www.website.com/foobar), to redirect back to this shorter URL.
So...
www.website.com/foobar/{controller}, when typed in, or clicked on in a bookmarked link, should redirect to www.website.com/foobar
I've tried playing around with the routing, but couldn't get anything to actually change the URL itself. I'm not sure if it's possible to change through mapping.
Edit: Has anyone used the HTTP Redirect within IIS7? I can redirect from the top tier, but cannot seem to get the redirect to work on any of the Views.
I don't think you can use routing alone to redirect up a domain. I had a similar issue and after a few days of searching and asking around, I ended up using Redirect itself to get over the issue. Hope this helps.
I was able to solve the problem in IIS 7 by using the HTTP Redirect module. Because it's an MVC application it was a little trickier to setup a redirect on a view, but it's possible. I used this tutorial: Creating a redirect in IIS7
Edit: While the above solution works, it tended to be unpredictable. I ended up solving it by reading the URL the user was inputting in the Index action of the Quote controller, and then redirecting if keywords I was looking for were in the URL. Just a simple if statement at the beginning of the Index action.

Why use MVC/router

I'm trying to comprehend the concept behind MVC and URL routing. I understand that it's good to seperate your code, hence MVC, but fail to understand the idea behind the URL router!
Instead of having a lot of rewrite rules in htaccess, I send all traffic to router.php, and in this page I have an array with page urls, and its corresponding PHP controller.
To keep it simple, I just include the controller, where the output finally is generated, however having seen lots of other practices, I'm afraid that im doing something wrong, or bad in some way..
Can someone please enlighten me, how to do a good, but simple URL router? Is it okay just to include the controller, which then generates the output? Perhaps someone has some information that describes the subject in details (something understandable for a beginner)
Thanks in advance
There are lots of ways to do URL routing. Some are client side like with backbone.js, others are server side. Doing it with .htaccess is one way, another is th way you are doing it by having a prerequisite path that is is either a hard path, or a regular expression that you parse and figure out where to send it. None of them are 100% right or 100% wrong, it's all preference, and it sounds like you are doing just fine with a route file.
For more information on how different frameworks do routing you should read over the docs on routing for CodeIgniter, and Symfony frameworks to see 2 different styles of server side routing, and then maybe look at the backbone.js framework for client side routing just to see the similarities and differences.
The router in the MVC concept decides which controller it has to load when a user requests a page. E.g. a user requests example.com/something/very/important, the router would now look for an action which is mapped to this route and execute it. There are different methods how you can accomplish that (simple include, instantiating a class and running a method etc.) but the most simple and still powerful solution I came up with is creating a separate class for every action. I've written a little article on that matter, since I've been asked this question several times, you can have a look at it here: Writing a simple and fast mvc router with PHP
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event.

Sitemesh different decorators for the same URL

I'm using urlrewriteFilter (org.tuckey.web.filters.urlrewrite.UrlRewriteFilter) to forward pages like www.mysite.com/myname to a Struts2 action. The action is mapped up in sitemesh, and it works properly.
But now I want to keep the same URL but apply another decorator to the page, based on whether the user is logged in or not.
I'm using AppFuse-stack Struts2.
Ok - since no-one else looks like having a go.
Sitemesh selects the decorators based on the incoming url string, so to have different decorators you need different urls depending on the login status of your client. AFAIK Sitemesh uses the entire Url string so this includes parameters so you might get away with appending ?loggedIn="true" or ?loggedIn="false" and map the decorators on this. However this doesn't help with POST requests.
Another way to do it would be to create two Struts packages - one for logged in users and one for anonymous users so your actions will have different paths and then map on the path part of the Url.
I don't know how practical this might be in your scenario, but a third option maybe to have one common decorator and control the layout via seperate stylesheets which you could control via a test in your jsp.
HTH
Regards

Static pages in MVC framework?

Where do you guys put your static pages, like "home", in an MVC framework? Do you have a "home" controller? A "pages" controller? Do you create actions for each static page?
I'm using CFWheels now, and I'm trying to figure out the best place to put them.
Edit: Apparently CFWheels doesn't require you to create actions for all your views. So you can just create an empty controller and call the views like actions, but not have to write out the blank functions.
CakePHP (and I guess, Ruby On Rails) has a "pages" controller. There is a routing function which redirects requests to /pages/foo to /pages/display/foo. Similarly, / is redirected to /pages/display/home. The display action looks up the views/pages folder for any file with a matching name and renders that.
At the end of the day, a static page is a view without a model, that was returned based on an action the user requested from your server by hitting particular route. :-)
Yes, technically you could expose the direct location of the view resource to the user and rely on the http daemon to go fetch it and return it. However, that means that the resource URL is now tied not to the semantic of the resource you want to expose, but to actual bits. This means that if you want another representation of that same resource, you have to expose it on a different URL.
So, when you create the structure of your web app, think first about the URLs and the resources you want to expose and then think how to implement each resource.
I put my static pages in the database using a simple CMS with a private admin page.
This way, the clients can make simple changes themselves.
In Wheels, you don't even need to create the controller file.
If you create your view here:
views/about/index.cfm
You don't need to create the controller file at all. Then you should be able to just call this with no problems:
http://www.example.com/about

Resources