Laravel基础知识
- public目录存放css,js等公共文件
- resources文件下的views目录存放的是视图文件
- routes文件下存放的是路由配置文件
- vandor 扩展类composer安装的扩展都会在这里显示
- database文件下存放数据库的migrations
Laravel生成需要的控制器
生成控制器
php artistan make:controller ArticlesController
生成model
php artistan make: make:model Articles
生成验证类
php artisan make:request CreateArticleRequest
记不清楚命令的可以输入 PHPsrtisan查看
E:\wamp64\www\blog>php artisan
Laravel Framework 5.4.36
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
clear-compiled Remove the compiled class file
down Put the application into maintenance mode
env Display the current framework environment
help Displays help for a command
inspire Display an inspiring quote
list Lists commands
migrate Run the database migrations
optimize Optimize the framework for better performance
serve Serve the application on the PHP development server
tinker Interact with your application
up Bring the application out of maintenance mode
app
app:name Set the application namespace
auth
auth:clear-resets Flush expired password reset tokens
cache
cache:clear Flush the application cache
cache:forget Remove an item from the cache
cache:table Create a migration for the cache database table
config
config:cache Create a cache file for faster configuration loading
config:clear Remove the configuration cache file
db
db:seed Seed the database with records
event
event:generate Generate the missing events and listeners based on registration
key
key:generate Set the application key
make
make:auth Scaffold basic login and registration views and routes
make:command Create a new Artisan command
make:controller Create a new controller class
make:event Create a new event class
make:job Create a new job class
make:listener Create a new event listener class
make:mail Create a new email class
make:middleware Create a new middleware class
make:migration Create a new migration file
make:model Create a new Eloquent model class
make:notification Create a new notification class
make:policy Create a new policy class
make:provider Create a new service provider class
make:request Create a new form request class
make:seeder Create a new seeder class
make:test Create a new test class
migrate
migrate:install Create the migration repository
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
migrate:status Show the status of each migration
notifications
notifications:table Create a migration for the notifications table
queue
queue:failed List all of the failed queue jobs
queue:failed-table Create a migration for the failed queue jobs database table
queue:flush Flush all of the failed queue jobs
queue:forget Delete a failed queue job
queue:listen Listen to a given queue
queue:restart Restart queue worker daemons after their current job
queue:retry Retry a failed queue job
queue:table Create a migration for the queue jobs database table
queue:work Start processing jobs on the queue as a daemon
route
route:cache Create a route cache file for faster route registration
route:clear Remove the route cache file
route:list List all registered routes
schedule
schedule:run Run the scheduled commands
session
session:table Create a migration for the session database table
storage
storage:link Create a symbolic link from "public/storage" to "storage/app/public"
vendor
vendor:publish Publish any publishable assets from vendor packages
view
view:clear Clear all compiled view files
基本操作
- 显示视图层
return view('welcome'); //加载页面
//多个变量输出到视图层
return view('index.about')->with(array(
'name'=>$name,
'sex'=>$sex
));
//一个参数变量输出
return view('articles.index')->with('article',$article);//articles控制器中的index方法
- 视图层文件每个控制器对应一个文件夹,后缀名为.blade.php
- 数据调用
{{$article->content}}//调用数据
if 判断语句和foreach循环语句
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
session调用
{{ Session::get('name') }}
文章分页调用
{{ $articles->links() }}
引入css文件js方法也是一样的
<link rel="stylesheet" href="{{ URL::asset('css/article.css') }}">
url链接,前面为方法,后面为传递的参数值
<a href="{{url('articles',$article->id)}}">文章一</a>
引入公共头部和底部
在views目录下面建立head.blade.php和footer.blade.php文件参考代码如下
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>关于我们</title>
</head>
<body>
<h1 style="text-align: center">我是头部</h1>
@yield('content')
<h1 style="text-align: center">我是底部</h1>
</body>
</html>
其他页面引入头部和底部文件
@extends('head')//继承head文件
在head文件中的content书写页面主体
@section('content')//开始
<h1 style="text-align: center">Article</h1>
@foreach($articles as $article)
<h2 style="text-align: center"><a href="{{url('articles',$article->id)}}">{{$article->title}}</a></h2>
<article style="text-align: center">
<div class="pull-right">
{{$article->content}}
</div>
</article>
<h5 style="text-align: center">{{ $articles->links() }}</h5>
@endforeach
@stop//结束
<link rel="stylesheet" href="{{ URL::asset('css/article.css') }}">
@extends('footer')//继承footer文件
- 控制器层代码编写
获取全部文章内容,根据id排序,分页。
public function index(){
$articles = Articles::orderBy('id', 'desc')->paginate(1);//模型查询
return view('articles.index')->with('articles',$articles);
}
获取单个文章的内容
$article = Articles::findOrFail($id);//模型查询
添加一篇文章CreateArticleRequest 为验证类,头部需要引入CreateArticleRequest类
引入Db类
use Illuminate\Support\Facades\DB;
use App\Http\Requests\CreateArticleRequest;
public function store(CreateArticleRequest $request){
$input = $request->all();
DB::table('articles')->insert($input);//数据库方式插入
return redirect('/articles');//跳转到指定的页面
}
验证类代码编写
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateArticleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title'=>'required',
'content'=>'required',//
];
}
}
显示验证未通过的代码
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
- 路由配置
Route::get('/','IndexController@index');// http://www.****.com = index控制器下的index方法
Route::get('/about/','IndexController@about');//http://www.****.com/about =index控制器about方法
Route::get('/articles/add','ArticlesController@add');//http://www.****.com/articles/add = articles控制器add方法
Route::get('/articles/{id}','ArticlesController@show');//http://www.****.com/articles/5 = id是5的文章