I am using cakephp 1.3. here's my problem:
I have a controller named "learns" and a method named "classroom".
To access the classroom method, I use this link: http://www.url.com/learns/classroom/15
I wanted it to be like this: http://www.url.com/class/15
And this is how I setup the routes:
Router::connect('/:class/:id', array('controller' => 'learns', 'action' => 'classroom'), array('id' => '[0-9]+'));
I don't really know why its not working though. I read through the documentation and I just copied this solution from the cookbook..
Thanks for the help in advance.
I don't see anything wrong with your Router statement. Though I am not sure if you actually wanted /:class/:id instead of "/class/:id". See the difference? A Colon is missing in the 2nd version.
This tells the Router that any request that begins with /class/[an-id] should be mapped to whatever your rule is. Where as having it as /:class means you are passing an argument to the router. It can be anything /foo/15 or /bar/15 and these arguments will be available in your $this->params['class'] and $this->params['id']., assuming this rule -> /:class/:id
In your question you state that "I don't really know why its not working". Please avoid these kind of statements as it does not say any thing about the actual problem.
Instead tell us what were you expecting? And what did you see instead? Was it an error? Or a Warning? If you see something else entirely, for example a different action got executed, it is probably due to the fact how routers actually work. If you had a greedy route and a normal route like this:
/users/* and
/users/:id
The second url will not be matched for any request as /users/* is greedy and will hog all the requests to itself, unless the first routing rule returns false.
If this is your situation I would suggest you read up on how to write custom route classes. In summary custom route classes try to make a greedy route less greedy. For a better explanation here is an excellent article by mark story: http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp
Routing in cakephp is one of the most confusing topics and might take a while before you get your head around it. Cookbook is your best friend. Read and Re-read everything until you are sure what each routing element does in a routing rule.
Related
The Problem:
I'd like a single ui-router state to match the following urls:
#/.../update?id
#/.../update?username
#/.../update?customerId&sandwichId
If/when the 'update' state is active (it's a routed component), the component's controller knows enough to expect which params will be there (via a provided array of Strings that should/will match whatever query params are in fact present).
Current Solutions (none of which I'm 100% happy with)
I could just leave the url at '#/.../update' with the implicit understanding that I cannot navigate to that url from anywhere but it's parent state, and just pass in its primary key fields as a component binding. The drawback obviously being if I just type the URL in my browser, it will not have enough information to load.
As per an example I found here, I defined my 'update' state with single query parameter: pkKeys. I then define pkKeys upon my state transition to be the entire query parameter string I desire. This effectively achieves what I am looking for. However, typing this just doesn't feel right:
#/.../update?pkKeys=username=test
Moreover, it looks ugly on the more complicated situations:
#/.../update?pkKeys=customerId%3D3ae2e6eb-3bf7-42f8-a09c-9c690c8dbe15%26sandwichId%3D2cb6d513-06a3-4aa4-93bb-e53d279d95cb
Ideal Target State
Ideally, I'm looking for a way to configure my state in a way that matches the above patterns of urls. I did not have much success with $location service, but something tells me that has something to do with it. It appeared to me that $location.search() was cleared whenever $state.go( ... ) was invoked.
Thanks for any help in advance! Happy to elaborate on anything that might not be clear. It's a rather unique problem set I find myself in here...
On Further Research
For reference, I'm using Angular UI Router 1.x. I have found they have the notion of dynamic parameters, which seems to nearly solve the problem I am encountering (see the GitHub issue for this change). However, where this fails me is the state configuration still expects you to list the dynamic parameter's name. As my query param names are data-driven, the set of names is not a finite set. So I need dynamic parameters...with dynamic names.
At the moment in my Angular app, I'm using URLs with the ID e.g.
mysite.com/story/34jkh4432jkdasddasdsadasdas3
For what reasons would it be beneficial if any to instead do something like:
mysite.com/story/34jkh4432jkdasddasdsadasdas3/the-story-title
Is it strongly recommended? (e.g. for helping SEO) (An obvious advantage i see is if the URL is shared, easier for people to know roughly what they are about to see).
Else wouldn't the URL just become really long?
Also to implement it - I presume I can technically ignore the 'slug title' as its just that long ID i use? It would just be there for SEO/user readability.
Is that the correct way to think of it?
I notice even on this page, if I remove anything after the post ID it still takes me to the correct page.
Thanks.
I'm working on a project that requires that most UI state is reproducible via URL. In a traditional (server-side) app, I could use both URL parameters like:
/resources/:id
and unordered optional query string parameters, like:
/resources/:id?page=5&sort=date
Is there an idiomatic way to achieve this with Backbone/Marionette routing? I don't want to have to configure routes for every possible combination of parameters.
The fact that I don't see this addressed much makes me think I may be barking up the wrong tree, approach-wise, but I do think being able to represent as much UI state as possible in the URL is pretty important to a lot of projects.
It looks like the best option is the now-orphaned backbone-query-parameters project.
It supports routes exactly in the form I'm looking for:
#resources/:id?flag=true
URL parameters are not really enforced by Backbone/Marionette. One possible reason is that URL parameters are not SEO friendly.
Instead, you can configure optional URL fragments which will work pretty much like URL parameters, this way:
/resources/:id(/page/:page)(/sort/:sort)
If you do this way, the only gotcha here is that this sequence of "parameters" need to be ordered.
HOWEVER if you need it to be unordered, you can simply use Regular Expressions with router.route() method inside your initialize, as explained in Router#route
I would like to format my URL like so:
/teacher/page:2
to
/teacher
I would like to achieve this result by using as less code as possible (perhaps only from routes.php?), without modifying how the PaginatorHelper behaves.
Thank you for your help!
You can accomplish this with:
ajax
form posts instead of gets (requires altering the pagination
helper)
But I would advise against it. This is designed for good usability. If you change it, It will make your site less user friendly than you may want it to be. I know I would be frustrated trying to jump to a specific page only to find out I have to click my way to page 35. Yuck!
whats the use of calling a cakephp function
http://localhost/project/controller/method.hello
so whats hello?
but it requires views/method/hello/method.ctp...
It seems there are some errors in your description.A typical CAKEPHP url should something like:
webroot/controller/method/argument
and a method must have a view file.See typical request of cakephp
As SpawnCxy noticed, maybe you did not understand well the request "philosophy".
Anyway, using routes you can define your own, but the default is
/:controller:/:function:[/:arguments:]
remember that you can also use Named Parameters