by Devin Yang
(This article was automatically translated.)

Published - 5 years ago ( Updated - 5 years ago )

Laravel 8 has undergone a major revision, and my website has also been revised accordingly. The "notes" and "articles" on my website were originally separated into two blocks.
This time I integrated him together. Anyway, there are not many articles, and the combined content seems to be more. :p

My definition of an article is that the content is more original.
The notes are recorded casually for future reference. The notes may be original or copied from the Internet.

This upgrade will be carried out in two days. The first day is to step on landmines to upgrade the background, from 5.7.28 to 8.8.0,
Since Laravel is full of clear errors, it is not difficult to modify. Only when changing the Routes in the background, read it while modifying.
People who have upgraded like me should understand what I'm talking about.
use App\Http\Controllers\UserController;

Route::get('/user', [UserController::class, 'index']);

Fortunately, my Vim marco has reached the point of proficiency, and it will be done in about three minutes. Press the blank to automatically rewrite,
I can't help but admire Vim's macro function again, which is really easy to use.

On another day, a major revision of the front-end layout was carried out, but everything was kept simple.

This article is just chatting, sharing how I changed it, not teaching how to upgrade Laravel.

How to upgrade Laravel, you can see my old article.
https://www.ccc.tc/article/how-to-update -ow-backend

This upgrade, because it is a large version, so my strategy is to install a new one directly, and then import my background into composer.json.

But in fact, you can try to adjust the version number in composer.json to upgrade. You can take a look at Laravel's Upgrade Guide.
https://laravel.com/docs/8.x/upgrade

Speaking of the Laravel version number, just a while ago a friend asked me how to get the Laravel version number, I will post it here by the way..
$laravel_version=app()::VERSION;
The following ow_source is my background, and it can be used by loading it through psr4.
 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Ow\\Model\\": "app/ow_source/seo-model/",
            "Ow\\": "app/ow_source/seo-class/"
        }
    },
Of course, the RouteServiceProvider needs to add my own background-specific routing
public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                -> middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::middleware('owadmin')
                ->namespace($this->namespace)
                ->group(base_path('app/ow_source/ow_routes.php'));

            Route::middleware('owapi')
                ->namespace($this->namespace)
                ->group(base_path('app/ow_source/ow_routes.php'));
        });
}

The above is far away, back to this article, the front-end code part, here I changed the block number (b_ids) to array,
Originally only displaying a single content can now directly display the content of multiple blocks. It is very easy to change Laravel somehow.
public function articles($bymonth){
$b_ids=[1,2]; //Block of the default home page
$queryStr = "";
if(!preg_match('/\d{4}-(\d?\d$)/u', $bymonth, $matches)){
return abort(404);
};
$bymonth.="-01"; //Add number 1
$queryStr.=sprintf(" AND c_dt BETWEEN '%s' AND DATE_ADD('%s', INTERVAL 1 MONTH)", $bymonth,$bymonth);
$contents = Content::ofBlock($b_ids)
-> whereRaw('1 = 1'.$queryStr)
->orderBy('c_id','desc')->get();
$archives = $this->archives($b_ids); //archive data
//return $archives;
return view("$this->frontend.pages.index",[
'contents'=>$contents,
'archives' => $archives
]);
}

Originally used where, but changed to whereIn, so it can be used @@, as you can see in the Model below, I changed it to whereIn.

In addition, in the content Model below, why do I need to customize the table and primaryKey? It’s definitely not that I’m too busy or looks more powerful.
It's because my backend existed before Laravel, so all the data table fields have already been defined,
This is the case of customizing $table and $primaryKey. Otherwise, basically, I will use Laravel's rules to call IDs, and then Table is plural.
 
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
class Content extends Model
{
protected $table='content';
protected $primaryKey='c_id';

public function scopeOfBlock($query, $b_ids)
{
    return $query->whereIn('b_id', $b_ids)->where('is_publish',1);
}


Another thing I probably learned from this revision is that the information I spit out is the same, so I shouldn’t change the version and even the Controller, right?
So, I pulled out the template folder and changed the dynamics. If I want to adjust different templates in the future, just change the frontend. (Start planning to build a multi-version front desk)
return view("$this->frontend.pages.index",[
    'contents'=>$contents,
    'archives' => $archives
]);

Here is my modified diff

After simplifying everything, it seems that the speed of the website has improved significantly, with a PageSpeed ​​Insights score of 98.


 

Tags: laravel

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


d-laravel, docker, docker-compose, laravel

D-Laravel released v0.9.1 version

In order to keep D-Laravel in an operational version and a stable version. Start tagging this version Pass those tests.. This version has passed the ubuntu real and macos real machine tests, and the Container can be successfully established and executed..

laravel,dotenv,seo

How do I dynamically load different dotenv files in Laravel according to the subdomain name

Before we start, let me complain. Originally, my website could automatically display different languages ​​according to the user's browser. That's okay, but Google's SEO doesn't seem to like it. He recommends using different URLs for each language and not using cookies or browser settings to adjust the content of the page. Well, I'll just be obedient. His suggested method, the first solution: distinguish by country, such as example.tw or example.de, how is this really impossible, or buy the registered domain name, or take all the domain names and no one will take it The strange domain name is more likely. The second solution: use sub-domain names to distinguish, this is what I am going to do, and so on to explain how to do it in Laravel. The third solution: example.com/tw/ and the like, Apple seems to do it this way. The fourth solution: site.com?loc=tw and the like are not recommended, indeed I think this is not a good idea.

laravel,letsencrypt,haproxy

PHPENV's HAProxy environment is set up with free SSL certificate application

PHPENV has added the support of haproxy.yml. If you have an external IP and domain name, it should be quite simple to get HAProxy and certificate application through deviny/phpenv. In this article, let us see how to use HAProxy in the HAProxy environment setting in PHPENV.