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

Installation #11

Closed
xLink opened this issue Apr 19, 2014 · 26 comments
Closed

Installation #11

xLink opened this issue Apr 19, 2014 · 26 comments

Comments

@xLink
Copy link

xLink commented Apr 19, 2014

Okay so i have installed the package and all that jazz, looks like its working fine. Problem i have is thusly...

Route::api(['version' => 'v1', 'prefix' => 'api'], function () {

    Route::get('test', function () {
        return ['data' => ['a','b','c']];
    });

});

So thats supposed to create a route on api/test that will return some json correct?

So anyway, i run 'php artisan routes' only to not see that route atall?

 $ php artisan routes
+--------+-------------------------+----------------------+---------+----------------+---------------+
| Domain | URI                     | Name                 | Action  | Before Filters | After Filters |
+--------+-------------------------+----------------------+---------+----------------+---------------+
|        | GET|HEAD _debugbar/open | debugbar.openhandler | Closure |                |               |
+--------+-------------------------+----------------------+---------+----------------+---------------+

Am i doing something wrong or what? Any help is greatly appreciated... currently using laravel 4.1.25

@jasonlewis
Copy link
Contributor

Ah, actually that's a good point. They probably won't show up in that
artisan command as they are registered separately to the standard Laravel
routes. Try hitting the route, you should get your JSON. Then create a
non-api route and do dd(API::get('test')); and you should get your original
array.
On 20 Apr 2014 01:53, "Dan Aldridge" notifications@github.com wrote:

Okay so i have installed the package and all that jazz, looks like its
working fine. Problem i have is thusly...

Route::api(['version' => 'v1', 'prefix' => 'api'], function () {

Route::get('test', function () {
    return ['data' => ['a','b','c']];
});

});

So thats supposed to create a route on api/ that will return some json
correct?

So anyway, i run 'php artisan routes' only to not see that route atall?

$ php artisan routes
+--------+-------------------------+----------------------+---------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------+----------------------+---------+----------------+---------------+
| | GET|HEAD _debugbar/open | debugbar.openhandler | Closure | | |
+--------+-------------------------+----------------------+---------+----------------+---------------+

Am i doing something wrong or what? really getting annoyed with these API
packages tried several of them and none of them seem to be working :S...
currently using laravel 4.1.25


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

LMAO damn, okay that helps a little bit (not sure why i didnt think to hit the url myself to check XD)

@jasonlewis
Copy link
Contributor

Good point you raised though. I might have to roll a custom api:routes
command that lists the API routes.
On 20 Apr 2014 02:16, "Dan Aldridge" notifications@github.com wrote:

LMAO damn, okay that helps a little bit (not sure why i didnt think to hit
the url myself to check XD)


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

Curiously, can you have multiple Route::api() blocks and have it register it all of them?

@jasonlewis
Copy link
Contributor

You can, yes. Usually you'd have a single group per version.
On 20 Apr 2014 02:24, "Dan Aldridge" notifications@github.com wrote:

Curiously, can you have multiple Route::api() blocks and have it register
it all of them?


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

Yeah Im trying to split my application up into modules, which means each module has its own set of routes, its own version/extension on the api and be able to consume the api from within also...but all im getting is a crap load of problems with it :(

@jasonlewis
Copy link
Contributor

Hm, I'd be inclined to keep them all the same version and only make a new
version when you need to introduce backwards incompatible changes to the
API. And what do you mean when you say extension?
On 20 Apr 2014 02:30, "Dan Aldridge" notifications@github.com wrote:

Yeah Im trying to split my application up into modules, which means each
module has its own set of routes, its own version/extension on the api and
be able to consume the api from within also...but all im getting is a crap
load of problems with it :(


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

okay for eg, This project is gonna have the following modules (eg)

forum/
auth/
admin/

each module will have its own api and routes setup, so really they all need to have their own Route::api() block

@jasonlewis
Copy link
Contributor

Yeah you can reuse the same version in multiple groups. Unless you've tried
and you can't? You should be able to though. It won't override existing
routes either.
On 20 Apr 2014 02:36, "Dan Aldridge" notifications@github.com wrote:

okay for eg, This project is gonna have the following modules (eg)

forum/
auth/
admin/

each module will have its own api and routes setup, so really they all
need to have their own Route::api() block


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

Okay ive been playing around, and before installing the package, i could see the following routes

 $ art routes
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+
| Domain | URI                                               | Name                       | Action                                                                  |
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+
|        | GET|HEAD _debugbar/open                           | debugbar.openhandler       | Closure                                                                 |
|        | GET|HEAD api/forum/{category}                     | pxapi.forum.category.view  | Cysha\Modules\Forum\Controllers\Api\V1\CategoryController@viewCategory  |
|        | GET|HEAD api/forum                                | pxapi.forum.category.index | Cysha\Modules\Forum\Controllers\Api\V1\CategoryController@getCategories |
|        | GET|HEAD forum/{category_name}/{thread_name}.html | pxcms.forum.thread.index   | Cysha\Modules\Forum\Controllers\Module\ThreadController@getThread       |
|        | GET|HEAD forum/{category_name}                    | pxcms.forum.category.index | Cysha\Modules\Forum\Controllers\Module\CategoryController@getCategory   |
|        | GET|HEAD forum                                    | pxcms.forum.index          | Cysha\Modules\Forum\Controllers\Module\CategoryController@getCategories |
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+

After adding the alias's back and renaming Route::group() to Route::api() with the version in there, all im seeing in the command is the debugbar route, none of my forum/ routes are showing, which should be as they are not under a Route::api() block

@jasonlewis
Copy link
Contributor

Paste your routes file.
On 20 Apr 2014 02:45, "Dan Aldridge" notifications@github.com wrote:

Okay ive been playing around, and before installing the package, i could
see the following routes

$ art routes
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+
| Domain | URI | Name | Action |
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+
| | GET|HEAD _debugbar/open | debugbar.openhandler | Closure |
| | GET|HEAD api/forum/{category} | pxapi.forum.category.view | Cysha\Modules\Forum\Controllers\Api\V1\CategoryController@viewCategory |
| | GET|HEAD api/forum | pxapi.forum.category.index | Cysha\Modules\Forum\Controllers\Api\V1\CategoryController@getCategories |
| | GET|HEAD forum/{category_name}/{thread_name}.html | pxcms.forum.thread.index | Cysha\Modules\Forum\Controllers\Module\ThreadController@getThread |
| | GET|HEAD forum/{category_name} | pxcms.forum.category.index | Cysha\Modules\Forum\Controllers\Module\CategoryController@getCategory |
| | GET|HEAD forum | pxcms.forum.index | Cysha\Modules\Forum\Controllers\Module\CategoryController@getCategories |
+--------+---------------------------------------------------+----------------------------+-------------------------------------------------------------------------+

After adding the alias's back and renaming Route::group() to Route::api()with the version in there, all im seeing in the command is the debugbar
route, none of my forum/ routes are showing, which should be as they are
not under a Route::api() block


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

// routes-api.php
<?php

// if the request matches the route group, or we are in console(easier to debug), add the routes
if (Request::is(\Config::get('core::routes.paths.api', 'api').'/*') || App::runningInConsole()) {

    Route::api(['version' => 'v1', 'prefix' => \Config::get('core::routes.paths.api', 'api')], function () use ($namespace) {
        $namespace .= '\Api\V1';

        // add forum resources, such as categories and threads
        Route::api(['prefix' => 'forum'], function () use ($namespace) {

            Route::api(['prefix' => '{category}'], function () use ($namespace) {
                Route::get('/', ['as' => 'pxapi.forum.category.view', 'uses' => $namespace.'\CategoryController@viewCategory']);
            });

            Route::get('/', ['as' => 'pxapi.forum.category.index', 'uses' => $namespace.'\CategoryController@getCategories']);
        });

    });
}
// routes-module.php
<?php

// if the request matches the route group, or we are in console(easier to debug), add the routes
// if (Request::is('forum/*') || App::runningInConsole()) {

    Route::group(['prefix' =>  'forum'], function () use ($namespace) {
        $namespace .= '\Module';

        // categories
        Route::group(['prefix' => '{category_name}'], function () use ($namespace) {

            // threads
            Route::group(['prefix' => '{thread_name}.html'], function () use ($namespace) {

                Route::get('/', ['as' => 'pxcms.forum.thread.index', 'uses' => $namespace.'\ThreadController@getThread']);
            });

            Route::get('/', ['as' => 'pxcms.forum.category.index', 'uses' => $namespace.'\CategoryController@getCategory']);
        });

        Route::get('/', ['as' => 'pxcms.forum.index', 'uses' => $namespace.'\CategoryController@getCategories']);
    });

// }

Both get included by a routes.php which is auto included from the module itself

@jasonlewis
Copy link
Contributor

I'll have a look at this properly when I'm at my computer. You should
probably use a regular route group instead of nesting Route::api like that
though.
On 20 Apr 2014 02:49, "Dan Aldridge" notifications@github.com wrote:

// routes-api.php<?php
// if the request matches the route group, or we are in console(easier to debug), add the routesif (Request::is(\Config::get('core::routes.paths.api', 'api').'/*') || App::runningInConsole()) {

Route::api(['version' => 'v1', 'prefix' => \Config::get('core::routes.paths.api', 'api')], function () use ($namespace) {
    $namespace .= '\Api\V1';

    // add forum resources, such as categories and threads
    Route::api(['prefix' => 'forum'], function () use ($namespace) {

        Route::api(['prefix' => '{category}'], function () use ($namespace) {
            Route::get('/', ['as' => 'pxapi.forum.category.view', 'uses' => $namespace.'\CategoryController@viewCategory']);
        });

        Route::get('/', ['as' => 'pxapi.forum.category.index', 'uses' => $namespace.'\CategoryController@getCategories']);
    });

});}

// routes-module.php<?php
// if the request matches the route group, or we are in console(easier to debug), add the routes// if (Request::is('forum/*') || App::runningInConsole()) {

Route::group(['prefix' =>  'forum'], function () use ($namespace) {
    $namespace .= '\Module';

    // categories
    Route::group(['prefix' => '{category_name}'], function () use ($namespace) {

        // threads
        Route::group(['prefix' => '{thread_name}.html'], function () use ($namespace) {

            Route::get('/', ['as' => 'pxcms.forum.thread.index', 'uses' => $namespace.'\ThreadController@getThread']);
        });

        Route::get('/', ['as' => 'pxcms.forum.category.index', 'uses' => $namespace.'\CategoryController@getCategory']);
    });

    Route::get('/', ['as' => 'pxcms.forum.index', 'uses' => $namespace.'\CategoryController@getCategories']);
});

// }

Both get included by a routes.php which is auto included from the module
itself


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

@xLink
Copy link
Author

xLink commented Apr 19, 2014

i tried that, didnt like it XD

// running api/forum
ErrorException

Argument 1 passed to Dingo\Api\Http\Response::makeFromExisting() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\JsonResponse given, called in /Users/danaldridge/Sites/cysha/vendor/dingo/api/src/Routing/Router.php on line 138 and defined

(
12. ErrorException
…/­vendor/­dingo/­api/­src/­Http/­Response.php25
)
// running forum/

RuntimeException

There is no API collection for the version "v1".

@xLink
Copy link
Author

xLink commented Apr 19, 2014

Any idea when you could have a look at this?

@jasonlewis
Copy link
Contributor

I'm still unable to recreate this. I have the following routes:

Route::api(['version' => 'v1', 'prefix' => 'api'], function()
{
    Route::group(['prefix' => 'forum'], function()
    {
        Route::get('/', 'ForumApiController@index');
    });
});

Route::group(['prefix' => 'forum'], function()
{
    Route::get('/', 'ForumController@index');
});

BOTH controllers are extending from Dingo\Api\Routing\Controller (I also had them BOTH extend from Illuminate\Routing\Controller) and it seems to work fine. Note that I'm not nesting Route::api in the first group. You shouldn't be doing that. Use a regular group. Only the outermost group should be a Route::api group and it should define the version and prefix.

@xLink
Copy link
Author

xLink commented Apr 20, 2014

okay gonna go try it again been a few hours since, but it is 3am now so hrm XD

@xLink
Copy link
Author

xLink commented Apr 20, 2014

Okay gone through the install again, can get to forum/ which seems to want to 404 if i have the API::get() call in there o.O on the api route itself i get

Argument 1 passed to Dingo\Api\Http\Response::makeFromExisting() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\JsonResponse given, called in /Users/danaldridge/Sites/cysha/vendor/dingo/api/src/Routing/Router.php on line 138 and defined

My routes file now looks like this...

<?php

$namespace = 'Cysha\Modules\Forum\Controllers';

// require_once 'routes-admin.php';
// require_once 'routes-api.php';
// require_once 'routes-module.php';

Route::api(['version' => 'v1', 'prefix' => 'api'], function () use ($namespace) {
    Route::group(['prefix' => 'forum'], function () use ($namespace) {
        $namespace .= '\Api\V1';
        Route::get('/', $namespace.'\CategoryController@getCategories');
    });
});

Route::group(['prefix' => 'forum'], function () use ($namespace) {
    $namespace .= '\Module';
    Route::get('/', $namespace.'\CategoryController@getCategories');
});

and checking artisan routes shows the module route

 $ art routes
+--------+-------------------------+----------------------+-------------------------------------------------------------------------+----------------+---------------+
| Domain | URI                     | Name                 | Action                                                                  | Before Filters | After Filters |
+--------+-------------------------+----------------------+-------------------------------------------------------------------------+----------------+---------------+
|        | GET|HEAD _debugbar/open | debugbar.openhandler | Closure                                                                 |                |               |
|        | GET|HEAD forum          |                      | Cysha\Modules\Forum\Controllers\Module\CategoryController@getCategories |                |               |
+--------+-------------------------+----------------------+-------------------------------------------------------------------------+----------------+---------------+

@jasonlewis
Copy link
Contributor

What are you returning from this API route?

@xLink
Copy link
Author

xLink commented Apr 20, 2014

Just some json. I know that works without the api stuff running, or do I just return the array and let it do its stuff?

@jasonlewis
Copy link
Contributor

Yeah don't return a JSON response. Return a regular array or object.

@xLink
Copy link
Author

xLink commented Apr 20, 2014

Okay so the API side works now, doing

    public function getCategories()
    {

        $api = API::get('api/forum');
        dd($api);

    }

im getting a 404 on the forum/ route, if i comment out the api & dd and echo something there, i can see the echo so API is doing something odd there

@jasonlewis
Copy link
Contributor

You don't need to specify the prefix. Just API::get('forum') should be all you need.

@xLink
Copy link
Author

xLink commented Apr 20, 2014

Holy crap, that took a lot longer than it should have, thanks so much for your help :D now, back to some real coding ^^

@jasonlewis
Copy link
Contributor

No worries! If you haven't already I suggest you read through the Wiki. I'll hopefully be doing a few screencasts on it in the coming week. 😄

@xLink
Copy link
Author

xLink commented Apr 20, 2014

Impressively i did, i think cause i went through about 4/5 different packages that supposidly did the same thing, It was just melting my brain :3 ill have another look though the wiki a bit later and maybe submit some changes perhaps

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

2 participants