Codeigniter and Laravel together

Who said you can’t mix and match between Laravel and code Igniter. well, you will face some issues, but they are not that hard to resolve.

It all starts with a cool package called EFTEC/BladeOne, which is simply blade without laravel !

to install it, you go to your CI application folder, and run the command

composer require EFTEC/BladeOne 

At this stage, we have Blade up and running, to use it, create a file under core for example, and let’s say we will call the controler /core/bladeController.php

redis for Laravel on Debian

If you are like me, running only your own projects on a server, you might want to skip authentication

the changes I generally make to the file /etc/redis/redis.conf are

1- At the very beginning, limit the RAM redis can use with the line

maxmemory 2gb

2- Change the supervisor to systemd by modifying the line

supervised no

to

supervised systemd

Now, to test the new config, from the command line, run the following commands

systemctl restart redis-server
redis-cli
ping
config get maxmemory
quit

You have just configured redis and tested your new settings.

Microsoft VS code plugins for the Laravel Developer

The following is a list of plugins I have installed to help with Laravel Development, I will add to them as I go, I will also remove the ones i don’t think were worth it from the list as well

PluginWhat forHow to usePublisher
PrettierVery popular plugin to format your code so that it looks tidyDo take a look at the plugin’s config to tweak it to your likingPrettier
PHP IntelliSenseAdvanced Autocomplete and Refactoring support for PHPFelix Becker
PHP IntelephenseCooler than PHP IntelliSenseBen Mewburn
Laravel Extra IntellisenseSelf explanatory name !
PHP Namespace ResolverImport and expand PHP namespacesMehedi Hassan
laravel-bladeLaravel blade syntax highlightingChristian Howe
Laravel SnippetsWinnie Lin
Laravel Blade SnippetsLaravel blade snippets and syntax highlight supportWinnie Lin
Laravel goto viewQuick jump to view !codingyu
Better AlignAlign code without selecting them firstwwm
Laravel Blade WrapperSpeed up wrapping code with Blade directivesCTRL + SHIFT + TlHunte
Laravel-goto-componentsTakes you to Blade components <x-… with a clickCTRL + CLICKnaoray
DotENVSyntax highlighting for .env files !mikestead
Tailwind CSS IntellisenseIf you use Tailwind (A css library), this plugin will do all the tailwind magickTailwind Labs

Laravel Tutorial: Blade part 1

The simplest Blade tutorial !

Disambiguation: This tutorial is about Blade, the template engine for Laravel…. I am covering blade on Laravel 9, but I am also taking into account that you might be maintaining code from blade 5 !

Why yet another blade tutorial ? they are all over the place !

long as this tutorial may be, it should be an easy read. no need to memorize stuff or write notes, I am trying to structure it in such a way so that you can effortlessly understand the concept, then come back for that one little syntax you have forgotten very easily.

Simply put, i think blade is a very simple framework that you can complicate if you insist, I think there is an easier way to get someone up and running with blade, a tutorial that compares the Blade way to the traditional way, does not invoke flashy abbreviations before explaining what they mean, and serves you the simplest explanation first… Also one that involves illustrations, shows the different features and explains how they differ and when to use features and why…

I’ll leave the flashy terms and history lessons near the end of the tutorial for completeness, and because they may be useful, but not before you are comfortable with Blade.

Why not a video tutorial

If this gets any attention, I will create a video from it, but I personally prefer reading and looking at illustrations.

What is blade in simple terms ?

Traditionally, when creating a website in PHP, your HTML is mostly in the same PHP files that provide the functionality ! blade is here to help you separate your HTML content from your PHP functionality…

Blade is a special folder in your Laravel project where you add those PHP files that contain your HTML*, plain old folder where you simply separate those files from the rest of your code. files in that folder enjoy extra functionality provided by blade that takes out the inconveniences of separating the HTML.

in your Laravel project, that folder is usually /resources/views, blade PHP files end in .blade.php, for example, mytheme.blade.php

* HTML And JSON if you are also writing an API…. and whatever you normally add to your HTML such as inline CSS and inline JS

Isn’t Separating my HTML into it’s own files a lot more work ?

Well, that is what blade is for, it provides tools to make this simple and easy

Traditionally, if you wanted to separate the HTML from the rest of your code, you would create PHP files with the HTML in them, and use PHP’s include function to show them… or embed the HTML inside functions that are in separate files, and send the data to those functions (or classes) and print what is returned…

That works, and blade is more or less the same thing, but as you proceed with Blade, you will see how blade resolves many inconveniences that this method comes with.

Let’s get down to it !

Setup Laravel on your system

First, I am assuming you have already setup Laravel and created a new project, if not, please take a look here (/2022/09/07/laravel-tutorial-laravel-setup/) for instructions on how to do that !

Create a couple of dummy routes

Now that you have such a setup, We will need to add a couple of routes (URL definitions), don’t concern yourself with what they are or what they mean, they are covered in the next section, they are just here to enable us to learn blade, routes is in a totally different place, all you need to learn for now that those two routes are invoked when you visit the URLs they define, and they pass the variables you see in them to the blade templates

Routes are added to the /routes/web.php file, so open that file and add the following at the end of it, now you have two URLs that work, the home page, and a /test page





First Blade File: plain HTML and nothing more

Now, let us create our first BLADE file, let us call it example.blade.php, and in that file, we will simply add an HTML page with nothing to do with blade specific features, Just that page that will display whether you use the home page (the first route from above), or the /test route that you see in the other route !

—————–

Unorganized content to be incorporated into the tutorial

If you stick to a couple of rules, it is compiled into PHP code and cached, so it is really fast !
Asset helpers
Layouts (extends, Yeild, section, show)
Partials
Components: includes, arrays vs collections {{$var->entry}}, props, &lt;x-cards>,

BLADE  is inspired by .NET’s Razor engine.

Allows you to use PHP code inside (@php directive), but you should not need to, and you should not unless you have to
(If you feel a need, you are misplacing your code)

If you come from Symfony, you can use Twig through Twig-bridge !

Syntax

1- Echo
{{ $variable }} is equivalent to <?= htmlentities( $variable ) ?>

Sometimes you may need to echo handlebar notation into the output, so you can simply use an @ before the above notation

@{{ $variable }} will output {{ $variable }} (Without the @)

or, if you want things to just print as they are, you can use the @verbatim directive !

{!! $variable !!} is equivalent to <?= $variable ?>


{{ $variable }} = <?= htmlentities( $variable ) ?> 
{!! $variable !!} = <?= $variable ?>
{{-- comment is here --}}



self explanatory blade directives

if elseif else endif (endif is the closing after all of them)

@unless and @endunless = equivilent to if(!cond)

@for, @foreach, and @while (endfor endforeach enwhile)

@forelse and @endforelse = ForElse is a ForEach loop, but with extra handling for empty array.

@forelse ($talks as $talk)
	{{ $talk->title }} ({{ $talk->length }} minutes)<br>
@empty
	No talks this day.
@endforelse

-----------------------------------------------------------
Defining Sections with @section/@show and @yield

yeilds first param is the section name, it's second is A DEFAULT VALUE

instead of yeild, if you want an entier block as a default fallback, you can use

@section('footerScripts')
<script src="app.js"></script>
@show

In this @section .... @show, if we want to append to the default value above, we should include @parent in the child template extending this, otherwise, the contents above will be overwritten !

Note that The @show is in the parent section, shows in place, while the @section and @endsection are in the child !

the section show is both defining a default and doing so in such a way that its default contents will be available to its children, through @parent

to use all of the above... assuming the template is at resources/views/layouts/master.blade.php
@extends('layouts.master') {{-- Each file should only extend one other file, and the @extends call should be the first line of the file --}}

@section('title', 'Dashboard')

@section('content')
     Welcome to your application dashboard!
@endsection

@section('footerScripts')
@parent
     <script src="dashboard.js"></script>
@endsection
-----------------------------------
partials



------------------------------------------------
1: creating a folder called images under public !

---------------------------------------------------

Method 1: @yeild

put the header and footer in one file called layout.blade.php in one peice (empty doc)

within the template, you put in 

@yield('content)

in other layout components, you use the extends directive, and make the content inside
 
 @extends(layout)
 
 @section('content')

whatever needed to appear in the theme, the content that is labeled content just like in yeild

@endsection

partials are added with include, so if you put the thing in the partials directory, the convention is that a file starts with underscore as a partial, _hero.blade.php

include('partials._hero)

Method 2: making the layout a component

---------------

in routes, read on route model binding, instead of passing $id to the inner function and route specification, we pass an object of type listing (The model listing) to the inner function, and in the route specification, the word /blah/{listing}, we  it allows us to just 

mysqldump by example

MySQL Dump is probably one of the best tool to take copies of databases, and it comes with MySQL, so you don’t need to install more stuff.

Example 1: Backup all databases (Use with caution, see below)
IMPORTANT: if you dump this to another server, you will lose all users on the target server, this is because the database named mysql (not the database engine but the actual database that has the users) on the target server is overwritten by the one from the source

Added note: If you want to monitor how large the uncompressed dump file has gone, or in other words, how much data mysqldump has brought so far, you can use PV, in this example, i expect the data to be 123GBs so i put that in so that PV can tell me what percentage of that i have finished, it will tell me the exact number of bytes anyways, but this is visually easier.

mysqldump --opt -u root --password="yourpass" databasename | pv -s 123g | pigz -c > dumpfile.sql.gz

 

1- Dump all databases

mysqldump -u root --password="thispassword" --all-databases > thisdatabasedump.sql

2- Dump all databases and gzip compress the output file, gzcompress is like compression used in zip files, this command compresses on the fly (make sure gzip is installed)

mysqldump --opt -u root --password="thispassword" --all-databases | gzip -9 > thisdatabasedump.sql.gz

3- Dump all databases and BZIP compress the output file, BZIP compression is better than gzip compression, but takes significantly more time, like the one before this command compresses on the fly (make sure you have bzip2 installed)

mysqldump --opt -u root --password="thispassword" --all-databases | bzip2 > thisdatabasedump.sql.bz2

4- If you have a server with multiple processors, you can overcome the slowness of bzip2 by simply making all the CPUs (real or virtual or hyper threaded) work on compressing at the same time, the application is called parallel bzip2 (make sure pbzip2 is installed)

mysqldump --opt -u root --password="thispassword" --all-databases | pbzip2 > thisdatabasedump.sql.bz2

4.5- If your server has 8CPUs and you only want 7 of them to do zipping so that one of them can be dedicated to mysqldump

mysqldump --opt -u root --password="thispassword" --all-databases | pbzip2 -p7 > thisdatabasedump.sql.bz2

I will not give any more examples about compression, obviously, as you can see from the examples above, to compress on the fly all you need to do is replace the section ( > thisdumpfile.sql ) with ( | pbzip2 > thisdumpfile.sql.bz2 )

5- Dump a certain database to the file

mysqldump --opt -u root --password="thispassword" thisparticulardbsname > thisdumpfile.sql

6- Dump certain databaseS

mysqldump --opt -u root --password="thispassword" --databases db1name db2name db3name db4name > thisdumpfile.sql

7- Dump certain tables from within a database

mysqldump --opt -u root --password="thispassword" databasename table1name table2name table3name > thisdumpfile.sql

8- Exclude certain tables from the mysqldump

 mysqldump --opt -u username --password="thispassword" databasename --ignore-table=databasename.table1 --ignore-table=databasename.table2 > database.sql

MySQL innodb row count very innacurate

They say the number of rows you see in PHPMyADMIN is approximate and not very precise for innodb tables, i can tell you that depending on how the table has been used, the results can be very very inaccurate and sometimes irrelevant.

phpmyadmin wants to display a row count with the list of tables, in myisam, a value is maintained within the database saying how many rows are in the database, INNODB does not maintain such a value so it has to either respond accurately without regard to the delay and scans required or give an approximate reading, it will depend on how you ask, read on for more details.

Q: why are innodb row counts innacurate in PHPMYADMIN ?

Well, innodb would return an accurate count if it were asked formally with a

SELECT COUNT(*) FROM tablename"

But phpmyadmin can not execute such a statement with every listing of a database, because it means the database engine will have to read the whole database and scan all rows.

Q: in that case how come it works for MyISAM ?

A: MyISAM maintains a number stating how many rows are in there that is incremented and decremented with every insert and delete, so the database engine does not need to scan the whole table to give us a row count.

Q: Then where does PHPMYADMIN get those approximate numbers ?

For speed, PHP MY ADMIN would execute something similar (If not exactly)

SHOW TABLE STATUS WHERE 1;

This is when MyISAM would read it’s internal row count value, and Innodb would return an estimate because it does not maintain such a value.

You can also use the command to see a subset of the tables like this

SHOW TABLE STATUS LIKE 'wp_%';

to see status of all tables starting with ‘wp_’

Q: Why does innodb not maintain such a number

A: probably for performance reasons, to avoid the need to update such a number with every insert and every delete.

Splitting INNODB files to separate .ibd files

Please note: most of you are probably visiting this post for the answer to whether you can use multiple disks with innoDB, the answer is, even though you can not move idb files around like when you move MYI MYD files, you can move an IDB file while the DB engine is not running, then create a symlink where the Innodb expected to find the original file, then start mysql again, so the short answer is that innodb does work with symlinks just like myisam.

The second answer is, you can not use more than one disk on the same table (even though you can for the same database by putting every table on a separate disk).

As you would probably know, innodb, by default stores all data into its central, single file data dictionary (on debian, /var/lib/mysql/ibdata1), you can ask innoDB to put every table’s data in a separate file (tablename.ibd) by simply adding the directive innodb_file_per_table anywhere under [mysqld] section.

Databases already in single file will stay there, the directive only affects the creation of new databases, InnoDB does not mind mixed mode regarding ibd files, dome tables in a single database can be in the shared file, and some can have their own files. files created while the directive is in effect will get separate files, you can then remove the directive from your my.cnf and the engine will start putting new databases back into the shared file.

Mind you, unlike ISAM tables, you can not move these files around, in MyISAM you can simply copy the 3 files that are any table, the .frm, .MYI and .MYD and you have your table elsewhere, in INNODB, this is not the case as much is still stored in INNODB’s ibdata1 , also, innodb files are connected to a certain database with internal strings, Yet, you can copy the whole data directory and have it work, remember, log files are also needed and must be kept.

To move an .ibd file and the associated table from one database to another, use a RENAME TABLE statement:

RENAME TABLE db1.tbl_name TO db2.tbl_name;

The main disadvantage of splitting data files per table is disk space, files in INNODB never get smaller, when in 1 file, other tables can use that space of deleted rows in another table, when split apart, you can only use the occupied space again if you add rows to the same table that had deleted rows.

The advantages are

1- You can utilize multiple disks for both space and performance (By using symlinks)
2- You can backup certain tables and leave others to add flexibility to your backup plan