Tagged: laravel-5

Applying version design in Laravel

Problem: Current Laravel need to serve different companies and have different stage(production, test, etc.)

Using Route::group and create a different folder for different website/stage.

Solution: follow post on this and this:

  1. Create folders for a different purpose and put controller you want inside it
/app
  /controllers
    /Api
      /v1
        /UserController.php
      /v2
        /UserController.php

In the UserController.php, remember to change namespace accordingly.

For example for controller under v1 folder

namespace Api\v1;

for controller under v2 folder

namespace Api\v2;

In the web.php, using Route::group to determine using which controller, like:

Route::group(['prefix' => 'api/v1'], function () {
  Route::get('user',      'Api\v1\[email protected]');
  Route::get('user/{id}', 'Api\v1\[email protected]');
});

Route::group(['prefix' => 'api/v2'], function () {
  Route::get('user',      'Api\v2\[email protected]');
  Route::get('user/{id}', 'Api\v2\[email protected]');
});

for any prefix like api/v1, using the controller under v1 folder.

Route::group(['domain' => 'itdailydocumentation.com'], function () {
  Route::get('user',      'Api\v3\[email protected]');
  Route::get('user/{id}', 'Api\v3\[email protected]');
});

for domain itdailydocumentation.com, using controller under v3 folder.

As for the version design for layout, you can also copy the view blade to a different folder:

views/
├── v1/
│   ├── master
|   |   ├── master-public.blade.php
|   |   ├── master-admin.blade.php
|   |   ├── master-user.blade.php
|   ├── views
|   |   ├── home
|   |   ├── chat
|   |   ├── order
|   |   ├── reports
├── v2/
└── v3/

Laravel does not load asset via HTTPS

Problem: After turn on HTTPS, Laravel load the assets via HTTP and it cause Google Chrome inspector to pop up a message “…was loaded over HTTPS, but requested an insecure…

Although you access the page via HTTPS but HTTPS page is asked to load a resource over HTTP. In general, simply change the setting in order to load assets via HTTPS.

Solution: for Laravel check out this post, below changes work for me.

Find AppServiceProvider.php and change boot()

    public function boot()
    {
        //
        If (env('APP_ENV') != 'local') {
            $this->app['request']->server->set('HTTPS', true);
        }

    }

If you are just for testing (local environment), simply change APP_ENV checking:

If (env('APP_ENV') == 'local') {

Laravel composer update failed with carbon extension

Problem: Run composer update failed with a message “providing support and update for 1.x versions, please upgrade to Carbon 2.”

This is because of carbon version 1 is deprecated, you have to use Carbon 2, but Carbon requires Laravel 5.8

Solution: According to the official Carbon website, you can just update composer.json with below content:

{
    "require": {
        "nesbot/carbon": "2.25.0 as 1.39.0"
        "kylekatarnls/laravel-carbon-2": "^1.0.0"
    }
}

In this case, you can still be using other versions of Laravel

Change Timezone in Laravel 5

Problem: In Laravel, the created_at and updated_at attribute in database value are incorrect when users trying to create and update data.

Solution: Change the timezone value in /config/app.php. The timezone list can be found at https://www.w3schools.com/php/php_ref_timezones.asp

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Taipei',  

419 error in laravel 5.6 when post route for testing

Problem: https://laracasts.com/discuss/channels/laravel/post-request-in-laravel-57-error-419-sorry-your-session-has-expired or just POST data to route. Having Error -- 419 Sorry, your session has expired.

Solution: If user just want to test the route using POST and don’t want to send csrf token, user can an exception for specific route in VerifyCsrfToken

VerifyCsrfToken .php

 
protected $except = [
'foo', // for route 'foo', this route can post without csrf token
'getReplytest', // for route 'getReplytest', this route can post without csrf token
];

405 error in laravel 5.6 using ajax

Problem: get error 405 when post URL

Laravel use csrf by default, you can add CSRF token in the form:

<form method="POST" action="/post_route" >
@csrf
...
</form>

But if you are using ajax to past data, you can add below code:

<!-- HTML-->
<meta name="csrf-token" content="{{ csrf_token() }}">
/* JQuery */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

laravel 5 only root route works/ index path works

Problem: Only ‘/’ route working --

Route::get('/', '[email protected]')->name('main.index');

Other routes return 404 using get and post method

Solution: Add below code to .htaccess in your project/ folder:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

There is no existing directory at “…/storage/logs” and its not buildable: Permission denied

Problem: put the whole laravel project folder to another directory. e.g /var/www/html/laravel-project to /var/www/laravel-project . After the update of the Virtual Host File. The issue appears when accessing the website

This is because the server still use and contain the old path record in cache.

Solution: Clearing Configuration Cache, Application Cache and Route Cache.

php artisan route:clear
php artisan config:clear
php artisan cache:clear