Annotations for URL

Now we have to define a pattern and his corresponding route in config file. But route depends on his action. Why dont we set URL in action comment with annotations?

In this way we can separate the logic of URL from controller name and action name. We will also able to move our action and the URL will work with no others modifications.

I mean something like this:




    /**

     * @Pattern("/post/show/{id}")

     */

    public function actionShow($id)

    {

        // ...

    }



or better:




    /**

     * @Pattern("/post/show/{id}")

     */

    public function blalblalblaName($id)

    {

        // ...

    }



Whit reflection we can see comments:




<?php

/** 

* A test class

*

* @param  foo bar

* @return baz

*/

class TestClass { }


$rc = new ReflectionClass('TestClass');

var_dump($rc->getDocComment())

?>



The above example will output:


string(55) "/** 

* A test class

*

* @param  foo bar

* @return baz

*/"

And we will can also read annotation and generate url for config file when It needed.

Imagine a large scale application…

your idea would mean that every time a new request is made we need to get the annotations of all the actions in all controllers to see if the request made is for any of them…

and if you want to change one special route… it would be a bit difficult to know at first where it’s exactly located.

  1. not every time: just when you create a new one. Yii can regenerate array configuration automatically. just once.

  2. if you want to change /a/route/like/this just search "/a/route/like/this".

  1. this introduces additional problems - when would that regeneration occur? how would Yii know that it’s a new one, which one is already generated which one is not, which one is changed ?

  2. so instead of doing like now - go to config.php and make the change… you would need to search for all controllers to find the route you need?.. and how would you search the route with parameters?

I think a developer know what happen in his own web application =).

Yes. Imagine to go to gii, to create a new controller, and in this moment regenerate route list. This can be automatic. And imagine to have a gii command o console command (just a click) to regenerate routes.

Look at this snippet:




class UserController {

    /* @url("/show/<username>"); */

    public function showByUsername ($username) {

    }

}



Either I don’t understand you or you don’t understand me :D (non ci cappiamo mica :P )

1 - the developer knows… but it’s Yii that needs to parse all the docs to generate the rules… if you create a new rule… how can Yii know which one you created?

and if you think that it should not know and just regenerate all the rules… again it’s not good… what if you did edit some of the previusly generated rules.

2 - again… so instead of just editing one line in the config.php (more simple IMO), you would need to go to gii or even command line to issue a command for re-generation… but that was not my point… my point was that if you want to change a specific route… you need to find the controller and action for it so to change/edit the doc that defines the route… a bit non intuitive if you use SEO rules where in the rule is neither the controller neither the action…

Tu non capisci me =). Io sto solo suggerendo un modo in più di fare le cose.

@ sensorario :

:lol: this is a good idea ; we can use cache but need a cache dependency every time (lets say the controllers dir changed )some change happens we need regenerate the rules array ;

and it 's not too difficult achieve your goal . use the annotation lib addendum

I don’t see any benefits in this way of declaring routes. Of course, it looks very cool and at the first sight it feels like it’s very simple since rule just sits on top of controller action but it will be very uncomfortable to work with such code for the reasons mdomba mentioned.

have a look @ http://www.recessframework.org/page/routing-in-recess-screencast

gii could offer an overview of all declared urls referencing the target controller/action.

would need to generate on each request till the app goes into production YII_DEBUG=false.

or/and for ex introduce a urlCached=true (false as default) to CUrlManager so this new feature could fallback to old behaviour.

gii could simply generate the rules array for the production purpose which u merge with main config.

dont know if its a must feature for the yii2 core. i fiddled around with such an implementation 2 years ago but didnt finish. the huge flaw is that one could never just touch the generated ruleset, as those changes wont be reflected in your doc comments. but mayb someone comes up with a smart move.

:)

beatmox

Is there any real benefit in adding all these tools and complexity to debug process?

Wasn’t (isn’t?) annotations meant to be used for pseudo-RTI (Runtime Type Information) ?

I fail to see what the benefits of using it with URL rules is…

Regex is not that slow, and if Yii could cache the URL rules, it would be for the better.

Using annotations for this is not what annotations was intended for. ;)

from my experience there was no real benefit, implementation felt hackish and yes the debug process would feel unusual.

i wouldnt vote for it as a core feature, but as i stated mayb someone has a vision and comes up with some awesome way.

@ jacmoe isnt yii capable of caching the url rules if u configured a cache component ?

:)

beatmox Yii is caching rules already.

this url annotation is very similar to java spring framework

R.K., yep. But that fact doesn’t make it any better.