Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API::route() #12

Closed
ryanhungate opened this issue Apr 22, 2014 · 26 comments
Closed

API::route() #12

ryanhungate opened this issue Apr 22, 2014 · 26 comments

Comments

@ryanhungate
Copy link

Hey Jason, any chance you wanted to build in support for the route() call vs clunky url patterns? :) I think it would be fantastic to do that, so in your internal API requests, they could be so clean.

@jasonlewis
Copy link
Contributor

You mean route names?
On 23 Apr 2014 07:02, "Ryan Hungate" notifications@github.com wrote:

Hey Jason, any chance you wanted to build in support for the route() call
vs clunky url patterns? :) I think it would be fantastic to do that, so in
your internal API requests, they could be so clean.


Reply to this email directly or view it on GitHubhttps://github.com//issues/12
.

@ryanhungate
Copy link
Author

Yeah, access the api route with the name for internal requests
On Apr 22, 2014 6:16 PM, "Jason Lewis" notifications@github.com wrote:

You mean route names?
On 23 Apr 2014 07:02, "Ryan Hungate" notifications@github.com wrote:

Hey Jason, any chance you wanted to build in support for the route()
call
vs clunky url patterns? :) I think it would be fantastic to do that, so
in
your internal API requests, they could be so clean.

Reply to this email directly or view it on GitHub<
https://github.com/dingo/api/issues/12>
.

Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-41114943
.

@jasonlewis
Copy link
Contributor

Will look into it.
On 23 Apr 2014 12:17, "Ryan Hungate" notifications@github.com wrote:

Yeah, access the api route with the name for internal requests
On Apr 22, 2014 6:16 PM, "Jason Lewis" notifications@github.com wrote:

You mean route names?
On 23 Apr 2014 07:02, "Ryan Hungate" notifications@github.com wrote:

Hey Jason, any chance you wanted to build in support for the route()
call
vs clunky url patterns? :) I think it would be fantastic to do that, so
in
your internal API requests, they could be so clean.

Reply to this email directly or view it on GitHub<
https://github.com/dingo/api/issues/12>
.

Reply to this email directly or view it on GitHub<
https://github.com/dingo/api/issues/12#issuecomment-41114943>
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-41117909
.

@jasonlewis
Copy link
Contributor

Implemented. 😄

@ryanhungate
Copy link
Author

Hey Jason, latest push does not find the routes by name? URL::route('bla')??

@jasonlewis
Copy link
Contributor

Any other details you can supply would be great. I'm about to head off for
the night so will investigate in the morning. Cheers!
On 6 May 2014 02:49, "Ryan Hungate" notifications@github.com wrote:

Hey Jason, latest push does not find the routes by name?
URL::route('bla')??


Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-42209267
.

@ryanhungate
Copy link
Author

Well, basically after you pushed that fix 9 days ago i think it was working, although this is kind of on the side project here... but after defining a route with a

   Route::get('blablabla', array( 'as' => 'bla', 'uses' => 'BlaController@bla' ));

then calling

   URL::route('bla'); 

it throws an error in the URL generator saying that the route is not defined.

@ryanhungate
Copy link
Author

Also, it seems as if when running:

   php artisan routes

that the urls are not showing up as well.

@jasonlewis
Copy link
Contributor

Is this to do with the API? What I pushed allows you to run API::route so
you can internally call routes using names.
On 6 May 2014 03:05, "Ryan Hungate" notifications@github.com wrote:

Also, it seems as if when running:

php artisan routes

that the urls are not showing up as well.


Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-42210944
.

@ryanhungate
Copy link
Author

That’s what I’m saying… it is throwing an error now when I try to create a URL the route name ( its for a javascript url in a blade file ). I don’t think I did anything to ruin this functionality, but Ill check.

If your saying that the routes dont get registered as they normally do, I guess I can understand that... but it kinda screws up my workflow with the way I use various urls in the application. I hate defining paths, because I tend to change them as I develop.

On May 5, 2014, at 10:06 AM, Jason Lewis notifications@github.com wrote:

sh

@jasonlewis
Copy link
Contributor

You probably can't use URL::route because these routes are not registered
in the actual route collection. So it should never have worked like that.
It was only meant for internal requests. I think I know what you're talking
about so will look into it in the morning.
On 6 May 2014 03:08, "Ryan Hungate" notifications@github.com wrote:

That’s what I’m saying… it is throwing an error now when I try to create a
URL the route name ( its for a javascript url in a blade file ). I don’t
think I did anything to ruin this functionality, but Ill check.

On May 5, 2014, at 10:06 AM, Jason Lewis notifications@github.com
wrote:

sh


Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-42211294
.

@ryanhungate
Copy link
Author

Cool man thanks a bunch. For what it's worth, i really like this package... Have a great night man.

@ryanhungate
Copy link
Author

If you dont think this is a good idea, scratch what I just asked for... I just think it makes sense that the routes be registered that way. Cleaner while your generating urls in your app with names and array params.

@ryanhungate
Copy link
Author

Jason, so i may not be doing something right, but im trying to access a route with :

    \API::route('get.my.route');
    /// in the controller also tried
    $this->api->route('get.my.route');

and its not working. Im getting a Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

But if I access the url in the browser, it works

I traced it and it seems like it finds the correct url by the route name right before queueRequest() is called.

@ryanhungate
Copy link
Author

Dont know what's really going on but it looks like you are comparing the current request to see if it's an API route in

    public function requestTargettingApi($request = null)

it is using the route in context... so if your hitting:
mysite/foo - and in the foo function your calling the api -

   \Route::get('foo', function() {
            $this->api->get('bar');
    }

its using /foo instead of bar.

@jasonlewis
Copy link
Contributor

Can you paste a sample of your routes? I have a named API route and using API::route seems to be working fine here.

@ryanhungate
Copy link
Author

Well it seems to be unrelated to route() in particular, because im getting
a not found error as soon as I call the api from the app, and not a url in
the browser
On May 8, 2014 6:23 PM, "Jason Lewis" notifications@github.com wrote:

Can you paste a sample of your routes? I have a named API route and using
API::route seems to be working fine here.


Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-42625127
.

@jasonlewis
Copy link
Contributor

Have you updated to the latest version recently? It was bumped to 0.3 so you may need to change that. Hopefully that helps.

@ryanhungate
Copy link
Author

Im on de5d1df

Also laravel 4.1.x-dev f3d1441

@ryanhungate
Copy link
Author

for the record... this returns me to the api route. - complete setup here

\Route::api(array('version' => 'v1', 'prefix' => 'api'), function()
{
    $version = 'Vextras\\Membership\\Api\\V1';

    \Route::get('plans/exists/{plan_id}/{plan_category}',array(
        'as'    => 'api.get.plan',
        'uses'  => "$version\\Plans\\PlansController@get"
    ));

    \Route::get('plans/assoc/{cat?}',array(
        'as'    => 'api.get.plans.assoc',
        'uses'  => "$version\\Plans\\PlansController@getAssociatedByCategory"
    ));

});

Route::get('tester', function() {
    try {
        dd(\API::get('plans/assoc'));
    } catch (Exception $e) {
        return Redirect::to('/api/plans/assoc');
    }
});
  • and I have tried it with route names, calling

    API::route('route_name'); 
    

@jasonlewis
Copy link
Contributor

Well I'm stumped. I copied your code exactly as you've pasted it and it works fine. Can access it using API::get and API::route, as well as in the browser.

@ryanhungate
Copy link
Author

Hey, I figured it out! It has to do with the prefix stuff in the

    Route::api(array('version'=>'v1','prefix'=>'api'));

if you set another prefix down the line, it overrides the previous prefix. I did a trace on it and the prefix was absolutely the culprit. So, usually I group things and use the prefix for a lot of my routes, to keep things clean, and in this case, I see that every API route grouping needs to follow 1 prefix pattern or it breaks. Try adding this next snippet underneath the previous code, simulating just loading in another api grouping somewhere in your app -

\Route::api(array('version' => 'v1', 'prefix' => 'users/{userID}'), function()
{
    \Route::get('/', function(){
        return array('success' => true);
    });
});

@jasonlewis
Copy link
Contributor

Yeah an API should use a single prefix (which is why there's a configuration option for it). You can still nest Route::group inside your Route::api, which is handy for other prefixes. 😄

@ryanhungate
Copy link
Author

yeah i totally get that now… I’m sure I can update things the right way now that I know why it was failing. I wasted a bit of time on that one! :) Thanks as always man, your great.

On May 9, 2014, at 7:31 AM, Jason Lewis notifications@github.com wrote:

Yeah an API should use a single prefix (which is why there's a configuration option for it). You can still nest Route::group inside your Route::api, which is handy for other prefixes.


Reply to this email directly or view it on GitHub.

@furey-imagination
Copy link

Is it possible to generate a URL for a dingo/api named route?

I've named an API route 'api.orders.show', and I can confirm that name was assigned by seeing it returned via php artisan api:routes.

When trying to generate the named route's URL via Laravel's route() helper, Dingo\Api\Routing\UrlGenerator throws an InvalidArgumentException:

"Route [api.orders.show] not defined"

Any ideas?

wayne5w referenced this issue Mar 17, 2016
Instead of using the existing Laravel router we will use a new instance. This
should prevent the APIs internal dispatcher from overriding the current route
on the actual Laravel router instance.

There does not seem to be any need for the router instance that is injected
into the adapter to be the actual Laravel router, as the adapter just needs
an instance to dispatch the API-only routes.
@scheMeZa
Copy link

I'm confused as to how I'm supposed to access these routes natively, this provider uses its own router which doesn't allow me to use other packages that makes routing accessible in javascript or with the simple route() helper function... What a pain in the arse ;(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants