Yii Framework Forum: Annotations for URL - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Annotations for URL

#1 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 21 May 2012 - 02:06 AM

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.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,339
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 May 2012 - 02:25 AM

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.
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 21 May 2012 - 05:19 AM

View Postmdomba, on 21 May 2012 - 02:25 AM, said:

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".
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,339
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 May 2012 - 05:51 AM

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?
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 21 May 2012 - 03:36 PM

View Postmdomba, on 21 May 2012 - 05:51 AM, said:

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 ?


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

View Postmdomba, on 21 May 2012 - 05:51 AM, said:

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?


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) {
    }
}

0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,339
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 May 2012 - 03:44 PM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 21 May 2012 - 07:48 PM

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

#8 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 586
  • Joined: 27-December 10
  • Location:china

Posted 21 May 2012 - 09:59 PM

@ 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
0

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 22 May 2012 - 04:50 PM

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.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#10 User is offline   beatmox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 05-February 09

Posted 23 May 2012 - 09:07 AM

have a look @ http://www.recessfra...cess-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.

:)
0

#11 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 24 May 2012 - 10:52 AM

beatmox
Is there any real benefit in adding all these tools and complexity to debug process?
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#12 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 May 2012 - 12:17 PM

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. ;)
"Less noise - more signal"
0

#13 User is offline   beatmox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 05-February 09

Posted 24 May 2012 - 01:28 PM

View Postsamdark, on 24 May 2012 - 10:52 AM, said:

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


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 ?


:)
0

#14 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 24 May 2012 - 01:48 PM

beatmox Yii is caching rules already.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#15 User is offline   R.K. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 20-September 10

Posted 24 May 2012 - 02:31 PM

this url annotation is very similar to java spring framework
0

#16 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 24 May 2012 - 04:36 PM

R.K., yep. But that fact doesn't make it any better.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users