Laravel

Создать переменную в определенном шаблоне при загрузке

app->Http>Providers->AppServiceProvider

public function boot(): void

    {

        View::composer('404', function ($view){

            $view->with('name', 'sergey');

            $view->with('age', 36);

        });

    }

Создать ограничение запросов к странице с одного IP

app->Http>Providers->RouteServiceProvider

 public function boot(): void

    {

        RateLimiter::for('main', function (){

            return Limit::perMinute(10);

        });

    }

    Route->web

    Route::view('/404', '404')->name('404')->middleware('throttle:main');

Добавление файлов через Vite

resources/js/app

import.meta.glob([

    '../img/**',

    '../fonts/**',

]);

Templete:

<img src="{{\Illuminate\Support\Facades\Vite::asset('resources/img/404.jpeg')}}" alt="">

Отправка push сообщения с Able

Устанавливаем:

composer require ably/ably-php

Для отправки сообщения создаем событие и вставляем код:

 public function __construct(string $name, string $message)
    {
        $client = new AblyRest('VgMOEw: ..........');
        $channel = $client->channel('channel');
        $channel->publish($name, $message);
    }

в bootstrap.js

const ably = new Ably.Realtime.Promise(' key ');

await ably.connection.once('connected');

console.log('Connected to Ably!');

const channel = ably.channels.get('channel');

await channel.subscribe('greeting', (message) => {
    alert(message.data)
});

async function sendMessage(message){
    await channel.publish('greeting', message);
}

// sendMessage('data').then(()=>{
//     console.log('ok');
// });

ably.close(); // runs synchronously
console.log('Closed the connection to Ably.');

Create makro for collection:

   Collection::macro('toMyMacro', function (){
            return $this->reject(function ($value){
                return is_string($value);
            })->reduce(fn($a,$b)=>$a+$b);
        });
       $res = collect(['name', 'age', 'color', 12, 45,6, 78,9])->toMyMacro();
Collection::macro('toLocale', function (string $locale) {
    return $this->map(function (string $value) use ($locale) {
        return Lang::get($value, [], $locale);
    });
});
 
$collection = collect(['first', 'second']);
 
$translated = $collection->toLocale('es');

Passport:

Установка:

composer require laravel/passport

php artisan migrate

php artisan passport:install


config/auth.php

'guards' => [

    'web' => [

        'driver' => 'session',

        'provider' => 'users',

    ],

    'api' => [

        'driver' => 'passport',

        'provider' => 'users',

    ],

],

Хеширование секрета клиента

App\Providers\AuthServiceProvider

boot

Passport::hashClientSecrets();

Время жизни токенов

public function boot(): void

{

    Passport::tokensExpireIn(now()->addDays(15));

    Passport::refreshTokensExpireIn(now()->addDays(30));

    Passport::personalAccessTokensExpireIn(now()->addMonths(6));

}

Утверждение запроса

php artisan vendor:publish --tag=passport-views


Клиент размещает у себя ссылку на наш сайт http://192.168.56.6/redirect.

Он должен быть добавлен у нас в системе и при переходе клиента указать в client_id свой id.

Route::get('/redirect', function (Request $request) {

    $request->session()->put('state', $state = Str::random(40));

    $query = http_build_query([

        'client_id' => '6',

        'redirect_uri' => 'http://192.168.56.6/callback',

        'response_type' => 'code',

        'scope' => '',

        'state' => $state,

         'prompt' => 'login', // "none", "consent", or "login"

    ]);

    return redirect('http://192.168.56.6/oauth/authorize?'.$query);

});

После авторизации выполнится обратный переход с данными нашего авторизованного клиента.

Route::get('/callback', function (Request $request) {

    $state = $request->session()->pull('state');

//    throw_unless(

//        strlen($state) > 0 && $state === $request->state,

//        InvalidArgumentException::class,

//        'Invalid state value.'

//    );

//    $response = Http::asForm()->post('http://192.168.56.6/passport/user', [

//        'grant_type' => 'authorization_code',

//        'client_id' => $request->user()->id,

//        'client_secret' => 'client-secret',

//        'redirect_uri' => 'http://192.168.56.6/callback',

//        'code' => $request->code,

//    ]);

    return \Illuminate\Support\Facades\Response::json( [

          'grant_type' => 'authorization_code',

        'client_id' => $request->user()->id,

        'email'=> $request->user()->email,

        'client_secret' => 'client-secret',

        'redirect_uri' => 'http://192.168.56.6/callback',

        'code' => $request->code]);

    return $response->json();

});

Обработка ошибки 404

App\Exceptions\Handler

public function render($request, Exception $exception)

{

    if ($this->isHttpException($exception)) {

        if ($exception->getStatusCode() == 404) {

            return response()->view('errors.404', [], 404);

        }

    }

    return parent::render($request, $exception);

}

Комментариев нет:

Отправить комментарий

Печать определенного фрагмента

 <!DOCTYPE html> <html> <head>     <title>Печать определенного фрагмента</title>     <style>         /* ...