Create Site
- Create project
1 | composer create-project laravel/laravel learnlaravel5 |
- Launch project
1 | php -S 0.0.0.0:1234 |
Then you can see the site with http://localhost:1234
- Add simple auth
1 | php artisan make:auth |
Add you can see the page as follow:
Then you need create database, configurate database .env file & migrate.
migrate command:
1 | php artisan migrate |
. Now you can register and login the system, but the home page need customer login. That because HomeController function __construct add auth as follow:
1 | public function __construct() |
Then we need change in file app/Http/Controllers/Auth/AuthController.php
1 | protected $redirectTo = 'admin'; |
php artisan make:model ModelName
1 | Then you will get a model class under folder app as follow: |
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ModelName extends Model
{
//
}
1 | * Create table for model |
php artisan make:migration create_modelname_table
1 | Then a file named ****\_create\_modelname\_table would be created under folder database/migrations. Modify the function up as follow: |
public function up()
{
Schema::create(‘articles’, function(Blueprint $table)
{
$table->increments(‘id’);
$table->string(‘title’);
$table->text(‘body’)->nullable();
$table->integer(‘user_id’);
$table->timestamps();
});
}
1 | Then migrant again with |
php artisan migrate
1 | You can see the table in database; |
php artisan make:controller SubFolder/NameController
1 | After that the contorller would be created under app/Http/Controllers. Add the function in the controller as follow: |
public function index()
{
return view(‘admin/article/index’)->withArticles(Article::all());
}
1 | For this controller, you need add *index.blade.php* under *resources/views/admin/article* to show data. If use the model in controller, you need add |
use App\Model;
1 | in the controller. |
Route::group([‘middleware’ => ‘auth’, ‘namespace’ => ‘Admin’, ‘prefix’ => ‘admin’], function() {
Route::get(‘/‘, ‘HomeController@index’);
Route::get(‘article’, ‘ArticleController@index’);
});
1 |
|
Route::resource(‘article’, ‘ArticleController’);
It's resource control in laravel, you will get 7 routes in project:
|verb|path|action|
|----|----|----|
|GET|/article|index|
| GET |/article/create|create|
|POST|/article|store|
| GET |/article/{article}|show|
| GET |/article/{article}/edit|edit|
|PUT/PATCH|/article/{article}|update|
|DELETE|/article/{article}|delete|
[example code](source/posts/code/1/learnlaravel5.tgz)