Danaq

Danaq

Member Since 1 Year Ago

Experience Points 590
Experience Level 1

4,410 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

11 Jul
1 week ago

Danaq left a reply on Custom Mail Log Channel Breaks MAIL_DRIVER=log

Thank you very much! It works as expected now.

From retrospective view it makes a lot of sense to me now :)

PS: The php_sapi_name()-function is only to prevent permission errors when log files are generated from cli as well as from http-requests. Maybe I wil change that to the permmission-attribute later but therefor I have to change my permission properties on my web-directories.

Thanks and have a nice day.

10 Jul
1 week ago

Danaq left a reply on Squareboat/sneaker Does Not Send Any Emails

Unfortunately not. I finally noticed, that the ~~~Mailable~~~-class used by the package, implements the ~~~ShouldQueue~~~-interface. During the process to include the package, I also setted up a queue in my system.

I was aware of the behavior that everything which implements this interface is automatically added to the queue and doing so never be processed because while in development I didnt setted up a proper queue worker, especially not for the default queue.

I have to check if I either change the package code to not queue the mailable or find out how I can change the queue these mailable are dispatched to.

Thanks for your input anyway ;)

Danaq started a new conversation Squareboat/sneaker Does Not Send Any Emails

Hi there,

my laravel 5.8 application will run on cli later. So, I found a packes which catches all exceptions and sends them per mail. I found https://packagist.org/packages/squareboat/sneaker Unfortunately, I don't receive any mails from this package.

While the manual execution of

Illuminate\Support\Facades\Mail::send(new Mailer($data));

works fine (using smtp), using the same approach in the package for testing purposes, does not change anything. The package originally includes

\Illuminate\Contracts\Mail\Mailer

and calls the process via:

$this->mailer->to($recipients)->send(new ExceptionMailer($subject, $body));

The testing command says everything works fine for the package besides I'm not getting any emails even at this point.

I'm not familiar in using contracts or facades nor building packages. I was just trying to debug this mailing functionality.

Does anyone have an idea?

Thanks very much!

Danaq started a new conversation Custom Mail Log Channel Breaks MAIL_DRIVER=log

I encountered an issue with the mail system of laravel 5.8. As the documentation says, Mail & Local Development one can change the way of sending emails to let it write them to a log-file.

Therefor, the .env as well as the config/mail.php-file have options for that. So, I added (the key was not here originally) a MAIL_LOG_CHANNEL-key to my .env-file with the value "mail" and changed the value of the key MAIL_DRIVER to "log".

Obviously, I added a new channel to the config/logging.php:

[...]
'mail' => [
            'driver' => 'single',
            'path' => storage_path('logs/mail-'.php_sapi_name().'.log'),
            'level' => 'info',
        ],
[...]

What ever I tried, the mail was never written to the seperate log-file. I testet the channel with a normal log-statement without any problem. I switched from MAIL_LOG_CHANNEL="mail" to MAIL_LOG_CHANNEL=mail added a default value to the section of the config/mail.php:

[before]
'log_channel' => env('MAIL_LOG_CHANNEL'),

[after]
'log_channel' => env('MAIL_LOG_CHANNEL', 'mail'),

not getting the width of a hair near to the log entry.

Only with the absolute basic configuration it actually writes the mail to the log but with this solution into the wrong log file.

Does anyone have an idea? It looks pretty strange to me.

Thanks for your help.

22 Jan
6 months ago

Danaq left a reply on Explicit Route-Model-Binding Dependency Error

Yeah, probably the approach with a policy is the best. I actually didn't thought about the use case to access those addresses as admin later. Because the application is currently not a situation to handle different user-types.

Thanks for your Help.

Danaq left a reply on Explicit Route-Model-Binding Dependency Error

I tried to use the aproach of working with a policy. Because I want to permit everything if the

address->user_id == user->id

I added this comparison to every method of the policy. At least this seems a bit messi.

After adding this policy to the AuthServiceProvider accoding to the documentation, It was not working. Even if I explicitly returned false in the update-method of the policy it falls through to the controller.

Maybe I misunderstood a part of the functionality here. After adding the policy to the AuthServiceProvider, how is the respective method in the policy executed, Are they maped to the model-events? Or do I have to call it manually again in each controller-method?

Danaq left a reply on Explicit Route-Model-Binding Dependency Error

To find a user by its ID is not the problem. But I wan't to achieve that laravel only finds addresses according to userID in the current URL. So, if the address exists, it also have to be an address of the user.

Danaq started a new conversation Explicit Route-Model-Binding Dependency Error

Hi,

I'm struggeling with an error in my RouteServiceProvider. I want to inject Models by URL-segements into my controller. Additionally the 404-error is nice to have if the ID is not present in the DB.


namespace App\Providers;

use Illuminate\Support\Facades\Route as Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use App;
   //[...]
    protected $namespace = 'App\Http\Controllers';

    public function boot()
    {
        parent::boot();

        Route::model('userID', App\User::class);
        Route::bind('addressID', function ($value, Route $route) {
            return App\Address::where([
                ['id', $value],
                ['user_id',$route->paramter('userID')]
            ])->firstOrFail();
        });
    }
//[...]

Unfortunately the injection of the Route-Class in the bind-closure throws an error that it is not of the proper class:

"Argument 2 passed to App\Providers\RouteServiceProvider::App\Providers\{closure}() must be an instance of Illuminate\Support\Facades\Route, instance of Illuminate\Routing\Route given"

I can't see the error because obviously the "Illuminate\Support\Facades\Route" Namespace is in use.

Can you give me a hint what is goin on here?

Thank you very much, have a nice day.

09 Mar
1 year ago

Danaq started a new conversation Recursive Dependency Injection In LoginController

Hi,

I ran into an issue with the reflection of Dependencies in my LogiController:

/LoginController

use App\Http\Controllers\Accounting;

class LoginController extends Controller
{
    use IssueTokenTrait;
    
    private $C_Account;

    public function __construct(Accounting\AccountsController $C_Account) 
    {   
        dd($C_Account);
    }
}
//AccountsController

namespace App\Http\Controllers\Accounting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class AccountsController extends Controller
{
    
    public function __construct() {
        
    }
}

When setting up my AccountsController that way, the Instance is correctly passed to the LoginController.

AccountsController {#190
  #middleware: []
}

However, if I add a Dependency to the AccountsController

//AccountsController

namespace App\Http\Controllers\Accounting;

use App\Http\Controllers\API\Accounting\AppAccountsController;

class AccountsController extends Controller
{
    
    public function __construct(
        AppAccountsController $C_AppAccount,
    ) {
        
    }
}

it throws a ReflectionException that AccountsController doesn't exists.

{
    "message": "Class App\Http\Controllers\Accounting\AccountsController does not exist",
    "exception": "ReflectionException",
    "file": "/var/www/html/ideegoapi/vendor/laravel/framework/src/Illuminate/Container/Container.php",
    "line": 811,
    "trace": [
        {
    //and so on

If I add another class-dependency (also with further dependencies in its constructor) to the LoginController everything works perfectly fine. The further dependencies will be resolved recursively.

What am I diong wrong here?

08 Mar
1 year ago

Danaq left a reply on Automatic Injection For Type-hinted Controllers

OK, I see there is a big problem with this approach. When simulating the call in tinker, the code is unbelievable long.

 $a = new AccountController(
    new AppAccountController(
        new App\Http\Controllers\Payment\CreditAccountController(
            new Account()
        )
    ),      
    App\Http\Controllers\Accounting\UserMetaController(
        App\Accounting\UserMeta()
    ),
    App\Accounting\AppAccount()
);

Additionally it can't be executed cause it doesn't find the UserMeta-Model (but it's there). And when calling the app with postman, the max_execution_time is exeeded.

I believe this approach is basically wrong but how this is solved with best-practice?

Danaq left a reply on Automatic Injection For Type-hinted Controllers

Could you explain that a bit more? I don't get it, sorry.

The make the thing I want to achieve a bit more clear: I want to seperate the the logic that selects data (done in the model-controller) from the logic which interacts with different models. My models are pretty small, I don't know if this is the right way. I often have to call different mothods from the model-controller. So from my perspective it makes more sense to have an instance of the model-controller instead of a "configured" instance of the model only received from the model-controller.

But I would be pretty happy for every improvement you can give me.

Danaq left a reply on Automatic Injection For Type-hinted Controllers

My AccountController is not directly associated with a model. I created controllers for all of my models. To sperate concerns, I decided, that my application will have some more controllers. Those additional controllers encapsulate logic which needs to get in touch with different other models. AccountController is one of these additional controllers.

To interact with a model, I'm calling the controller, associated to this model. In this case, the Account Controller has to create different records which are extending the user-account. But those records are not necessary for a basic register which also exists in the application.

Danaq started a new conversation Dependency Injection

OK, so I added a ServiceProvider to bind classes for DI.

//ControllerServiceProvider

public function register()
    {
        $this->app->bind(AppAccountController::class, function($app) {
            return new AppAccountController(); 
        });

But when this class is created I want that it has access to its model:

//AppAccountController

public function __construct(
                Account $C_Account, 
                UserMeta $C_UserMeta,
                AppAccount $appAccount
    ) {
        $this->C_Account = $C_Account;
        $this->C_UserMeta = $C_UserMeta;
        $this->appAccount = $appAccount;
    }

Because of this constructor, the code in the ControllerServiceProvider is not working so I changed it:

//ControllerServiceProvider

$this->app->bind(AppAccountController::class, function($app) {
    return new AppAccountController(
                new Account(),
                new UserMeta(),
                new AppAccount()            ); 
});

Sorry for the naming, it's probably a bit confusing.

AppAccount = Model Account = Model AppAccountController is the Controller for the AppAccount-Model

My models also have a ServiceProvider. There, I added some logic to modify them on creation.

//ModelServiceController

$this->app->bind(AppAccount::class, function($app) {
            if(defined('APP_ACCOUNT_ID')) {
                return AppAccount::find(APP_ACCOUNT_ID);
            } else {
                return new AppAccount();
            }
        });

Here I differ between an instance with values and just an instance of the Model.

When creating the AppAccountController I want, that this logic from ModelServiceProvider is applied for the creation of the AppAccount-Model.

Because I type-hinted this instance in the constructor of the Controller, I have to pass an instance even when creating it in the ServiceProvider, right? Or is there another way?

I'm finally not sure if I got this ServiceProvider thing in the actual right way. But thank you very much for you answers and help.

Danaq started a new conversation Automatic Injection For Type-hinted Controllers

Hi,

after understanding the very basics of ServiceProviders in Laravel I wanted to add this type-hinting-feature to my project. That's beacause I want to avoid multiple variations of this this kind of code. 2 lines for just one single method.

$deviceC = new DeviceController();
$device = $deviceC->create($appAccount->id, $request);

I read that just injecting instances of classes in the constructor is made automatically by laravel using reflextion.

https://code.tutsplus.com/tutorials/how-to-register-use-laravel-service-providers--cms-28966

"There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection."

I tried it that way:

use App\Http\Controllers\API\Accounting\AppAccountController;
use App\Http\Controllers\API\Accounting\DeviceController;
use App\Http\Controllers\Payment\BillDataController;
use App\Http\Controllers\Payment\ProductsController;
use App\Http\Controllers\Accounting\SubscriptionsController;


/**
* MODELS
*/
use App\Accounting;

class AccountController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Account Controller
    |--------------------------------------------------------------------------
    |
    | This Controller handels the creation process of different records for a 
    | new app-account when a user is registering or login for the first time.
    |
    */
    
    private $C_AppAccount;
    private $C_Device;
    private $C_BillData;
    private $C_Product;
    private $C_Sub;
        
    public function __construct(
                    AppAccountController $C_AppAccount,
                    DeviceController $C_Device,
                    BillDataController $C_BillData,
                    ProductsController $C_Product,
                    SubscriptionsController $C_Sub
    ) {
        
        $this->C_AppAccount = $C_AppAccount;
        $this->C_Device = $C_Device;
        $this->C_BillData = $C_BillData;
        $this->C_Product = $C_Product;
        $this->C_Sub = $C_Sub;
    }
    

But this was not working. The classes could not be found.

After binding them inside a service provider, everything worked.

So my questions are:

  • Why are controllers not automatically type hinted like custom requests? When entering a controller method and the is used in the class-file as well as type-hinted in the method, I can access the right type of my custom request-class withour adding any kinf of ServiceProvider.

  • What is the tutorial-post mean with the quoat? Did I missed anything?

  • Is there a best practice to distribute the bindings/ binded classes to the ServiceProviders? Currently I'm using a ServiceProvider for Controllers, another one for Models and so on.

07 Mar
1 year ago

Danaq left a reply on Add Middleware To Package Routes

Fortunately they'r using just the throttle-middleware on this route. I'll try that, and give report later.

06 Mar
1 year ago

Danaq started a new conversation Add Middleware To Package Routes

I know this was asked befor but many of the results I got are more than one year old, so maybe something has changed.

I'm using passport with laravel 5.5.

In the AuthServiceProvider I register the Routes for this package

public function boot()
    {
        $this->registerPolicies();
        
        //registers routes from passport-package
    Passport::routes();
        
        Passport::enableImplicitGrant();
        //configures the expire time of passport access-tokens 
        Passport::tokensExpireIn(Carbon::now()->addDays(config('auth.APP_ACCESS_TOKEN_EXPIRE_IN')));

        //configures the expire time of passport refresh-tokens 
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(config('auth.APP_REFRESH_TOKEN_EXPIRE_IN')));
    }

But I only need the oauth/token route. However I need it with additional middleware. Currently I added it to the api-routes. But when something is changes, I have to be aware of the complete fail of my api, I know.

TLDR: How can I add middleware to a package-registered route?

Danaq left a reply on Passport - Password Grant Token Using Custom Request

No I dodn't find a solution.

I tried many different ways

public function issueToken(Request $request, $grant_type, $scope = '*') {
 $params = [
            'grant_type' => $grant_type,
            'client_id' => $this->client->id,
            'client_secret' => $this->client->secret,
            'username' => $request->username ?: $request->email,
            'password' => $request->password,
            'scope' => $scope,
        ];
        
        $request->request->add($params);        
        $proxy = Request::create('api/oauth/token', 'POST');

        return Route::dispatch($proxy);

This is my current token-method called from the LoginController. The route the request is forwarded to is now directly written to the api-routes-file. I know in case of an update I have to adapt this route but I only use this only route and I wanted to add more middlewares to it because I need imformation from those even in the forwarded request. The login-mehtod there looks like this

public function login(Request $request) {
        $request->validate([
            'email' => 'required|string|email|max:255|',
            'password' => [
                'required',
                'string',
                'min:8',
                Rule::notIn([$request->email])
            ],
            'device_id' => 'required|alpha_num|max:255',
            'uuid' => 'required|alpha_num|max:50',
            'built_version' => 'required|alpha_num|max:50'
        ]);
        
        $accountSet = new Controllers\AccountController();
        $accountSet->registerUserForApp(Auth::id(), $request);
        
        return $this->issueToken($request, 'password');
    }

As you can see I'm using the default Request object now.

Here the variations I tried ($params is always set):

    $proxy = CustomRequest::create('api/oauth/token', 'POST');
    $proxy->request->add($params);

        return Route::dispatch($proxy);
//issueToken receives an instance of CustomRequest as $request
    
    $proxy = $request::create('api/oauth/token', 'POST');
    $proxy->request->add($params);

        return Route::dispatch($proxy);

I also tried to change the request-url in the given CustomRequest-object and pass this one to the dispatcher... nothing.

Everytime I use another instance of Request then the default one, the issueToken(ServerRequestInterface $request)-method of the oauth/token route receives a request-object without the data I added in the loginController.

Anywhere inside this magic laravel-thing this data is stored for the next request, I believe.

Danaq started a new conversation Laravel & Passport Using Custom Request

According to my other question which could probably help others to configure their passport, I could isolate the problem.

https://laracasts.com/discuss/channels/laravel/passport-in-laravel-55-grant-type-not-supported

    public function login(ApiRequest\AppLoginRequest $request) {
        return $this->issueToken($request, 'password');
    }

As you can see here I'm using a custom request in the login-method called by the first request on the api.

This method calls another method included via a trait into the loginController

public function issueToken(Request $request, $grant_type, $scope = '*') {
        $params = [
            'grant_type' => $grant_type,
            'client_id' => $this->client->id,
            'client_secret' => $this->client->secret,
            'username' => $request->username ?: $request->email,
            'password' => $request->password,
            'scope' => $scope,
        ];
        
        $proxy = $request::create('oauth/token', 'POST');
        $proxy->request->add($params);
// dd($proxy);
        return Route::dispatch($proxy);
    }

First, I'm creating a new request. This one has no values in the request-attribute. But it' created from the "old" request-object inside $request which is a custom request.

After that the $params-array is added to the "new" request. When I DumpAndDie the $proxy variable, this is shown:

AppLoginRequest {#244
  #container: null
  #redirector: null
  #redirect: null
  #redirectRoute: null
  #redirectAction: null
  #errorBag: "default"
  #json: null
  #convertedFiles: null
  #userResolver: null
  #routeResolver: null
  +attributes: ParameterBag {#269
    #parameters: []
  }
  +request: ParameterBag {#247
    #parameters: array:6 [
      "grant_type" => "password"
      "client_id" => 1
      "client_secret" => "5SMtQmPo2wKyt6OxWZvRQIhG2lw4n2zxMRxW1nMo"
      "username" => "[email protected]"
      "password" => "myPassword"
      "scope" => "*"
    ]
  }
  +query: ParameterBag {#248
    #parameters: []
  }
  +server: ServerBag {#271
    #parameters: array:17 [
      "SERVER_NAME" => "localhost"
      "SERVER_PORT" => 80
      "HTTP_HOST" => "localhost"
      "HTTP_USER_AGENT" => "Symfony/3.X"
      "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      "HTTP_ACCEPT_LANGUAGE" => "en-us,en;q=0.5"
      "HTTP_ACCEPT_CHARSET" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      "REMOTE_ADDR" => "127.0.0.1"
      "SCRIPT_NAME" => ""
      "SCRIPT_FILENAME" => ""
      "SERVER_PROTOCOL" => "HTTP/1.1"
      "REQUEST_TIME" => 1520355343
      "PATH_INFO" => ""
      "REQUEST_METHOD" => "POST"
      "CONTENT_TYPE" => "application/x-www-form-urlencoded"
      "REQUEST_URI" => "oauth/token"
      "QUERY_STRING" => ""
    ]
  }
  +files: FileBag {#272
    #parameters: []
  }
  +cookies: ParameterBag {#270
    #parameters: []
  }
  +headers: HeaderBag {#273
    #headers: array:6 [
      "host" => array:1 [
        0 => "localhost"
      ]
      "user-agent" => array:1 [
        0 => "Symfony/3.X"
      ]
      "accept" => array:1 [
        0 => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      ]
      "accept-language" => array:1 [
        0 => "en-us,en;q=0.5"
      ]
      "accept-charset" => array:1 [
        0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      ]
      "content-type" => array:1 [
        0 => "application/x-www-form-urlencoded"
      ]
    ]
    #cacheControl: []
  }
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: null
  #requestUri: null
  #baseUrl: null
  #basePath: null
  #method: null
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isForwardedValid: true
  pathInfo: "/oauth/token"
  requestUri: "oauth/token"
  baseUrl: ""
  basePath: ""
  method: "POST"
  format: "html"
}

So everything needed is there. The next target is also present.

The oauth/token route points to [email protected]

This method looks like this:

    public function issueToken(ServerRequestInterface $request)
    {
        dd($request);
        return $this->withErrorHandling(function () use ($request) {
            return $this->convertResponse(
                $this->server->respondToAccessTokenRequest($request, new Psr7Response)
            );
        });
    }

While this is a package-file, I changed nothing here except the dd(). So, they're using another kind of request here. When I dump it

ServerRequest {#327
  -attributes: array:3 [
    "hasAppAccount" => true
    "isSubscriber" => false
    "subDevice" => false
  ]
  -cookieParams: array:1 [
    "apidev_session" => "eyJpdiI6IkFxYXJIUmM0U2g3OGtoOXlzRmZpMkE9PSIsInZhbHVlIjoiS0tRK2h1OHRORWFNcGE2SmJweFJnTG9CV3UzdDB0bjlWTjN2YzRPc0hzZkxIcHRGeVZueU01cDQxeWJcL0hSS3RJOEVcL2FPNXFYV3ZNdit1MXVYTENmUT09IiwibWFjIjoiMjQ4YWRiZjc4ZDhhYzQ1NDQwNGYyMjExM2YyOGY0YzRjNzdjYzYzYjI5NDhhNDk4MDI5MWE5NmMyNDNhZTcxNiJ9"
  ]
  -parsedBody: array:6 [
    "email" => "[email protected]"
    "password" => "myPassword"
    "device_id" => "1"
    "uuid" => "1"
    "built_version" => "1"
    "newsletter" => "1"
  ]
  -queryParams: []
  -serverParams: array:30 [
    "DOCUMENT_ROOT" => "/var/www/html/ideegoapi/public"
    "REMOTE_ADDR" => "192.168.56.1"
    "REMOTE_PORT" => "56362"
    "SERVER_SOFTWARE" => "PHP 7.0.22-0ubuntu0.16.04.1 Development Server"
    "SERVER_PROTOCOL" => "HTTP/1.1"
    "SERVER_NAME" => "192.168.56.101"
    "SERVER_PORT" => "8080"
    "REQUEST_URI" => "/api/1/login"
    "REQUEST_METHOD" => "POST"
    "SCRIPT_NAME" => "/index.php"
    "SCRIPT_FILENAME" => "/var/www/html/project/public/index.php"
    "PATH_INFO" => "/api/1/login"
    "PHP_SELF" => "/index.php/api/1/login"
    "HTTP_HOST" => "192.168.56.101:8080"
    "HTTP_CONNECTION" => "keep-alive"
    "CONTENT_LENGTH" => "644"
    "HTTP_CONTENT_LENGTH" => "644"
    "HTTP_ACCEPT" => "application/json"
    "HTTP_CACHE_CONTROL" => "no-cache"
    "HTTP_ORIGIN" => "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop"
    "HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    "HTTP_POSTMAN_TOKEN" => "ab863501-5eab-5c05-7a96-a35ce8fea658"
    "CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryxtIQOdKajUI8BYQ2"
    "HTTP_CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryxtIQOdKajUI8BYQ2"
    "HTTP_DNT" => "1"
    "HTTP_ACCEPT_ENCODING" => "gzip, deflate"
    "HTTP_ACCEPT_LANGUAGE" => "en,de-DE;q=0.9,de;q=0.8,en-US;q=0.7"
    "HTTP_COOKIE" => "apidev_session=eyJpdiI6IkFxYXJIUmM0U2g3OGtoOXlzRmZpMkE9PSIsInZhbHVlIjoiS0tRK2h1OHRORWFNcGE2SmJweFJnTG9CV3UzdDB0bjlWTjN2YzRPc0hzZkxIcHRGeVZueU01cDQxeWJcL0hSS3RJOEVcL2FPNXFYV3ZNdit1MXVYTENmUT09IiwibWFjIjoiMjQ4YWRiZjc4ZDhhYzQ1NDQwNGYyMjExM2YyOGY0YzRjNzdjYzYzYjI5NDhhNDk4MDI5MWE5NmMyNDNhZTcxNiJ9"
    "REQUEST_TIME_FLOAT" => 1520355546.0966
    "REQUEST_TIME" => 1520355546
  ]
  -uploadedFiles: []
  -method: "POST"
  -requestTarget: "/api/1/login"
  -uri: Uri {#328
    #allowedSchemes: array:2 [
      "http" => 80
      "https" => 443
    ]
    -scheme: "http"
    -userInfo: ""
    -host: "192.168.56.101"
    -port: 8080
    -path: "/api/1/login"
    -query: ""
    -fragment: ""
    -uriString: null
  }
  #headers: array:13 [
    "host" => array:1 [
      0 => "192.168.56.101:8080"
    ]
    "connection" => array:1 [
      0 => "keep-alive"
    ]
    "content-length" => array:1 [
      0 => "644"
    ]
    "accept" => array:1 [
      0 => "application/json"
    ]
    "cache-control" => array:1 [
      0 => "no-cache"
    ]
    "origin" => array:1 [
      0 => "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop"
    ]
    "user-agent" => array:1 [
      0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    ]
    "postman-token" => array:1 [
      0 => "ab863501-5eab-5c05-7a96-a35ce8fea658"
    ]
    "content-type" => array:1 [
      0 => "multipart/form-data; boundary=----WebKitFormBoundaryxtIQOdKajUI8BYQ2"
    ]
    "dnt" => array:1 [
      0 => "1"
    ]
    "accept-encoding" => array:1 [
      0 => "gzip, deflate"
    ]
    "accept-language" => array:1 [
      0 => "en,de-DE;q=0.9,de;q=0.8,en-US;q=0.7"
    ]
    "cookie" => array:1 [
      0 => "apidev_session=eyJpdiI6IkFxYXJIUmM0U2g3OGtoOXlzRmZpMkE9PSIsInZhbHVlIjoiS0tRK2h1OHRORWFNcGE2SmJweFJnTG9CV3UzdDB0bjlWTjN2YzRPc0hzZkxIcHRGeVZueU01cDQxeWJcL0hSS3RJOEVcL2FPNXFYV3ZNdit1MXVYTENmUT09IiwibWFjIjoiMjQ4YWRiZjc4ZDhhYzQ1NDQwNGYyMjExM2YyOGY0YzRjNzdjYzYzYjI5NDhhNDk4MDI5MWE5NmMyNDNhZTcxNiJ9"
    ]
  ]
  #headerNames: array:13 [
    "host" => "host"
    "connection" => "connection"
    "content-length" => "content-length"
    "accept" => "accept"
    "cache-control" => "cache-control"
    "origin" => "origin"
    "user-agent" => "user-agent"
    "postman-token" => "postman-token"
    "content-type" => "content-type"
    "dnt" => "dnt"
    "accept-encoding" => "accept-encoding"
    "accept-language" => "accept-language"
    "cookie" => "cookie"
  ]
  -protocol: "1.1"
  -stream: Stream {#319
    #resource: stream resource @22
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    #stream: null
  }
}

So my forwarded proxy-request from above changed to this one where no request-data is present.

I still get the error

{
    "error": "unsupported_grant_type",
    "message": "The authorization grant type is not supported by the authorization server.",
    "hint": "Check the `grant_type` parameter"
}

when calling the login-route.

Is there anyone with an idea?

Danaq left a reply on Passport In Laravel 5.5 - Grant_type Not Supported

Ok, I have an update to this:

When i callthe Route oauth/token directly with this body:

username: [email protected]
password: myPassword
grant_type: password
client_id: 1
client_secret: 5SMtQmPo2wKyt6OxWZvRQIhG2lw4n2zxMRxW1nMo
scope: *

Then the reply is correct.

So this is probably the problem?

$request->request->add($params);        
$proxy = Request::create('oauth/token', 'POST');
return Route::dispatch($proxy);

How can I fix this? Is there an adaption necessary to be laravel 5.5 conform?

Danaq started a new conversation Passport In Laravel 5.5 - Grant_type Not Supported

Hi there,

like my last question here I hope you can help me again.

Im using Laravel 5.5 with passport 4.0

I already followed the documentation: https://laravel.com/docs/5.5/passport#password-grant-tokens

But I will only use the method to grant tokens by credetials.

Because the connection is used by a smartphone I setted the configuration in the

//AuthServiceProvider ´´´php public function boot() { $this->registerPolicies();

    Passport::routes();
    
Passport::enableImplicitGrant();

    Passport::tokensExpireIn(Carbon::now()->addDays(config('APP_ACCESS_TOKEN_EXPIRE_IN')));

    Passport::refreshTokensExpireIn(Carbon::now()->addDays(config('APP_REFRESH_TOKEN_EXPIRE_IN')));
}

´´´

like this. I added the 'enableimplicitGrant' cause the documentation called that as best practice for conecitons to mobile-devices.

I used the artisan commands

´´´bash php artisan passport:client --password php artisan passport:keys ´´´

The rest of my configuration looks like this:

//config/database.php ´´´php 'mysql' => [ [...] 'prefix' => '', 'strict' => true, 'engine' => 'InnoDB', //changes from 'null' ], ´´´ //config/auth.php ´´´php 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ],

    'api' => [
        'driver' => 'passport', //changed from 'token'
        'provider' => 'users',
    ],
],

´´´ //config/app.php ´´´php /* * Package Service Providers... */ Laravel\Passport\PassportServiceProvider::class, //added ´´´ And the User-Model was extended:

//User.php ´´´php [...] use Laravel\Passport\HasApiTokens;

class User extends Authenticatable { use HasApiTokens,Notifiable; [...] ´´´

So the login-Request as well as the register-request enters the particular controller

//loginController.php ´´´php class LoginController extends Controller { use IssueTokenTrait;

private $client;

public function __construct() 
{   
    $this->middleware('guest')->except('logout');
    $this->client = Client::find(1); //there is only one client present in the database
}

public function login(ApiRequest\AppLoginRequest $request) {
    return $this->issueToken($request, 'password');
}

public function refresh(ApiRequest\AppLoginRequest $request) {
    $this->validate($request, [
        'refresh_token' => 'required'
    ]);

    return $this->issueToken($request, 'refresh_token');
}

public function logout(Request $request) {
    $accessToken = Auth::user()->token();

    DB::table('oauth_refresh_tokens')
        ->where('access_token_id', $accessToken->id)
        ->update(['revoked' => true]);

    $accessToken->revoke();

    return response()->json([], 204);
}

} ´´´

The used IssueTokenTrait - The params-array should be built according to the documentation.

//IssueTokenTrait.php ´´´php public function issueToken(Request $request, $grant_type, $scope = '*') { $params = [ 'grant_type' => $grant_type, 'client_id' => $this->client->id, 'client_secret' => $this->client->secret, 'username' => $request->username ?: $request->email, 'password' => $request->password, 'scope' => $scope, ];

    $request->request->add($params);
    $proxy = Request::create('oauth/token', 'POST');

    return Route::dispatch($proxy);
}

´´´

But every time I only get the response: ´´´json { "error": "unsupported_grant_type", "message": "The authorization grant type is not supported by the authorization server.", "hint": "Check the e `grant_t parameter" } ´´´

I already rummaged in the venfor/laravel/passport folder and the vendor/league/oauth2-server/ folder to find where the error-message is thrown. I found it but it doesn't help me.

Is there someone who can help me with that?

01 Mar
1 year ago

Danaq started a new conversation Response Macros: Using Mthod Of ServiceProvider

Hi I'm new here but using laracast for serveral weeks now for very helpful answers. So thank you for your support already.

I'm trying to exclude my responses from the controllers to seperate them in one single place. Because I'm building an API for a mobile app and it would be very helpful if I didn't have to search through a lot of controllers to find the response which was used to terminate the current request. I'm using a chain of controllers to stay in the "seperation of concerns" repeatedly.

To concentrate my response-content to one single place I created a new config-file where the following structure is used:

// app/config/responses.php

'loginFailed' => array( 'message' => 'login failed', 'headers' => array( 'status' => 403 ) ),

I already found this, where the use of a ServiceProvider and Response macros was presented as a best practice: https://jamesmcfadden.co.uk/customising-api-responses-in-laravel-5

So with the support of the documentation I adapted this solution to my Laravel-Version 5.5 and created a ServiceProvider

//ResponseServiceProvider

class ResponseServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void / public function boot() { Response::macro('loginFailed', function ($data) { return $this->setResponseData('loginFailed', $data); }); } /* * Register the application services. * * @return void */ public function register() { // }

private function setResponseData($configName, $additionalData) {
    //get response content from configuration
    $config = config('responses.'.$configName);
    //get header from response content
    $header = $config['headers'];
    //delete header-content from response content
    unset($config['headers']);
    //apply additional data to response content
    $customFormat['data'] = $additionalData;

    return Response::make($customFormat)->header('status', $headers['status']);
}

So what I want to achieve is that the the same structure is applied to every response I'm fireing with a macro. But I get an error when using the private method inside the macro definition cause the scope was already moved to the Macro-Trait I guess.

So my questions are:

  1. How can I apply the same logic to the creation of a response macro every time?

  2. Can I access the first argument of the macro static method with the callback so that I don't have to repeat the name to find the right entry in the config-file?

  3. Is there a possibilty to make this whole process completely dynamic?

For example:

// SomeController

public function someControllerMethod() { return response()->someMacroName($additionalData); }

// ResponseServiceProvider

public function boot() { Response::macro([someMacroName], funciton($data) { ...and so on.... }

}

Probably you will pull apart my approach anyway ;)

Thanks a lot for your answers.