mercredi 7 mars 2018

Laravel routes with slash /

I use Laravel 5.1.

at route I have:

Route::get('/getVoucherRld', 'LiveController@getVoucherRld');

Route::get('/test/getVoucherRld', 'TestController@getVoucherRld');

both getVoucherRld functions are the same but when I try to run then with ajax

function from LiveController works fine but function at TestController just retunr me white screen?

I think the issue is with the route but why? Does anyone have the same issue?



via Chebli Mohamed

How to redirect login plus previous URL in laravel

                                        Laravel 5.1

I'm trying to build this functionality for every method in my controller. And this is very unpractical to do and very difficult to maintain. How can I set this that is when I registered an auth middleware in a specific route then it will redirect into a login page together with the URL that trying to view/visit.

Example:
URL: public/users

If this URL will try to visit by an unauthenticated user, then the user will be redirected to a url like public/login?url=http://localhost/myproject/public/users

Then After the user loggedin successfully, the user then will be redirected into http://localhost/myproject/public/users

What I have now: (But I think not a good one to use)

public function getLoginURL(){
    if(!Input::get('location-login')){
        // URL is not set, redirect user to home.
        return redirect('/');
    }

    if(auth()->check()){
        // User is logged in. Go the specified URL (depends on the middleware of the url (route))
        return redirect()->to(Input::get('location-login'));
    }

    if(!auth()->check()){
        // Redirect user to a login with the URL provided
        return view('pages::auth.login.url');
    }
}



via Chebli Mohamed

How to add item to an array inside a collection in Laravel

I have a Laravel Request object (collection) and there is an array inside this (address array) I want to add a item to this array. I tried $request->address['state'] = 'test'; and following error occurred.

Indirect modification of overloaded property Illuminate\Http\Request::$address has no effect

Here is the request collection

I want to add a item like highlighted in this img



via Chebli Mohamed

dimanche 4 mars 2018

Laravel 5.1 dont receive JWT token from Dingo API

I use Laravel 5.1 + Dingo API + JWT token. My route is:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function($api) {

$api->post('authenticate', 'App\Http\Controllers\AuthenticateController@authenticate');
$api->post('logout', 'App\Http\Controllers\AuthenticateController@logout');
$api->get('token', 'App\Http\Controllers\AuthenticateController@getToken');
$api->get('test', 'App\Http\Controllers\BitemsController@test');

});

$api->version('v1', ['middleware' => 'api.auth'], function ($api) {

    $api->get('authenticated_user', 'App\Http\Controllers\AuthenticateController@authenticatedUser');
    $api->get('getvoucher', 'App\Http\Controllers\BitemsController@index');
    $api->post('/store', 'App\Http\Controllers\BitemsController@store');
    $api->put('/update/{key}', 'App\Http\Controllers\BitemsController@update');

});

Now when I run at Postman to get token everything is fine, so I get:

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig"
}

but now I want to retrieve auth user and I have function protected by middleware, but I get:

{
    "message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
    "status_code": 401,
    "debug": {
        "line": 113,
        "file": "/home/dgadmin/public_html/test/vendor/dingo/api/src/Auth/Auth.php",
        "class": "Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException", ... ETC...

What is a problem here and how to solve this issue?

Interesting when I make the call like :

https://app.example.com/api/authenticated_user?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig

then everything is fine... but when I send token as Authorization header with Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig then I got the 401 error from above...

Why Laravel won't accept authorization header with my token?

I try to add .htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On



    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^ - [E=HTTPS_AUTHORIZATION:%{HTTPS:Authorization}]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]




</IfModule>

as you can see I add RewriteRule ^ - [E=HTTPS_AUTHORIZATION:%{HTTPS:Authorization}] but nothing happened

How to solve this?



via Chebli Mohamed

Root Security Issue

simple question :if you make post route and try to access it from google chrome it give me No Message error example :

Route::post('/Login', 'User@Login');

localhost/Login

if i access it from Postman as post request it give what i want

if i access it from Postman as get request

or

if i access it from GoogleChrome (" which is Get Request ")

it return No message error

what i want to redirect any one try to access any route as get to 404notfound page

attached image for the error enter image description here



via Chebli Mohamed

samedi 3 mars 2018

Can't send email with laravel 5.1 + gmail

I'm having troubles sending e-mails via a Laravel 5.1 application using gmail.

Here is my config file :

 <?php
    return [
            'driver' => 'sendmail',
            'host' => 'smtp.gmail.com',
            'port' => 578,
            'from' => array('address' => 'adresse@gmail.com', 'name' => 'Something'),
            'encryption' => 'tls',
            'username' => 'someaddress@gmail.com',
            'password' => '****',
            'sendmail' => '/usr/sbin/sendmail -bs',
            'pretend' => false,

    ];

Allowing less secure apps to access your account is turned on.

What config shall I use ?



via Chebli Mohamed

BadMethodCallException in Controller.php line 283: Method [] does not exist

I move my app from a server to another server, so from an IP address to other. I everything copy/paste including database, users ...

Now when I type example.com/venue/1 everything works fine, but when I type example.com/dng/1 app return me the error:

BadMethodCallException in Controller.php line 283: Method [dng] does not exist.

I run command php artisan route:list and everything is fine so all method including /dng{id} is here...

WHen I run php artisan route:cache I got: enter image description here

What is a problem? At old server everything was fine...



via Chebli Mohamed