Laravel – Remove Public from URL using htaccess

https://hdtuto.com/article/laravel-remove-public-from-url-using-htaccess

Step 1: Rename File

In first step it is very easy and you need to just rename file name. you have to rename server.php to index.php at your laravel root directory.

server.php
INTO
index.php

Step 2: Update .htaccess

first of all you have to copy .htaccess file and put it laravel root folder. You just copy .htaccess file from public folder and then update bellow code:

.htaccess

Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

Laravel All Routes Returning 404

Replace your .htaccess file with the below solution contents

Solution:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
  AddType application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>

Laravel New Project

use this one through composer:
composer create-project laravel/laravel

This is an older one I tried previously use the one above first:
composer create-project --prefer-dist laravel/laravel blog "5.8.*"

Laravel Install or Update

composer global require "laravel/installer=~1.1"

Since I installed laravel/installer globally via

composer global require laravel/installer

So I update laravel/installer globally via:

composer global update laravel/installer

Then do:

composer global show

and I see

laravel/installer             v2.3.0  Laravel application installer.

Now I can install a fresh Laravel with auth flag!

laravel new project --auth

This command:

laravel new my-test-app --auth

Is the shorthand of four following commands:

Laravel new my-test-appcd my-test-appcomposer require laravel/ui --devphp artisan ui vue --auth

To initialize the Laravel project you need to:

php artisan migratenpm install
npm run dev

Laravel Route With Null-able Parameter

When making a route you can set a null-able parameter, this will save you in terms of routing like doing an if, and if the parameter is there or not you can then perform your actions based on those parameter rules.

You would write is like so.

Not null-able does not have a Question Mark

Route::get('home'/{action}, function($action){
     return $action;
});

Null-able does have a Question Mark

Route::get('home'/{action?}, function($action){
     return $action;
});

Laravel Same Time Create Model and Controller

--------------------------------------
To create your model and controller together you can do this.
Method 1 : Does not set controllers as resource items.
php artisan make:controller CustomersController --model=Customer

Method 2 : Sets controllers as resource items.
php artisan make:model Todo -mcr

--------------------------------------
-m, --migration Create a new migration file for the model.
-c, --controller Create a new controller for the model.
-r, --resource Indicates if the generated controller should be a resource controller

--------------------------------------
For reference sake this is the method where you manually create each method for Model and Controller

The --resource sets up by default the controllers crud functionalities

php artisan make:model Customer
php artisan make:controller CustomersController --resource

https://stackoverflow.com/questions/43187735/laravel-5-4-create-model-controller-and-migration-in-single-artisan-command

The below is an example from Laravel.

php artisan make:model Flight --factory
php artisan make:model Flight -f

php artisan make:model Flight --seed
php artisan make:model Flight -s

php artisan make:model Flight --controller
php artisan make:model Flight -c

php artisan make:model Flight -mfsc