Laravel自学笔记

1.下载安装

composer下载项目文件

composer create-project --prefer-dist laravel/laravel blog

下好的文件如下

标红的vender是扩展,本身是没有这个文件夹的,需要composer install下载,但是我在下载的时候遇到了问题

先是乱码,后来百度了一下说是要升级一下composer到最新版?但是我本来就是最新版了,本着死马当活马医的态度composer selfupdate了一下,途中也遇到乱码,最终确认是最新版,然后再下载,倒是不乱码了,但是依然有问题

是不是没开啥扩展?

看了下文档,发现有几个好像是没开

标红的是我点开的,现在下载就正常了

 但是之后报错了,没有安装git,安装一下再放到path里面,重启就可以继续了。

当下载完毕之后,持续报错

No application encryption key has been specified
E:phpstudy_proWWW	estblog>cp -a .env.example .env
'cp' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

E:phpstudy_proWWW	estblog>php artisan key:generate
Application key [base64:CE7bYsvNwXP8SmAqkQSsPo4mc0A+3FI2mgv24Tc0+gQ=] set successfully.

E:phpstudy_proWWW	estblog>php artisan serve
Laravel development server started: <http://127.0.0.1:8000>
[Fri Aug  2 14:28:32 2019] 127.0.0.1:50740 [200]: /favicon.ico

 输入mark的命令,这样就可以了

跟着,就能看到美美的laravel 的初始化界面

是不是美美哒。

2.输出hello world

据说tp5是抄袭的不完整的laravel,应该也是有命令行的,在文档里面有,通过php artisan可以执行各种命令,比方说我们来生成一个控制器:

php artisan make:controller WelcomeController

生成的文件会在这里(路径是appHttpControllersAuth)

命名空间是AppHttpControllers 这个tp5跟这个是一样的。

 PS:可以在命令行只输入php artisan然后回车,系统会输出这个东西能够做什么


list Lists commands
migrate Run the database migrations
optimize Optimize the framework for better performance (deprecated)
preset Swap the front-end scaffolding for the application
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:exception Create a new custom exception class
make:factory Create a new model factory
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:resource Create a new resource
make:rule Create a new validation rule
make:seeder Create a new seeder class
make:test Create a new test class
migrate
migrate:fresh Drop all tables and re-run all migrations
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
package
package:discover Rebuild the cached package manifest
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

...

像这样。

早前就一直听说laravel是必须配置路由的,这也是我为什么一直没有get的原因——因为懒。

于是乎为了配合视图输出还是挣扎了一阵子的,原因在于没有搞清楚laravel的目录结构,搞清楚之后就好很多,但是由于我写错了路由,所以一直不出现我心心念念的hello world

应该是简简单单的这样:

Route::get('/', 'WelcomeController@index');

但是我写成了这样:

Route::get('/', function () {
    return view('WelcomeController@index');
});

所以我一直在被view not found支配着,也好在因为这个马虎的错误,让我了解了一下这个目录结构,views目录在

这里

controllers目录在

这里

 我寻思应该还有个models,这个东西应该在哪里呢?写个东西试试吧,增删改查走一遍。

用的是mysql,熟练地建造了一个msg的表,emmm,这个东西的表前缀应该在某config里面,或者也在database里面,对了

加上我的专属前缀~

 我的gsy_msg表


会遇上一些问题:

1)数据库连接失败:我用的mysql , database已经确认过无误,结果还需要改env的内容

这样一改就连接正常了

2)找不到方法,找不到视图

  路由是个需要费力了解的东西啊,跟tp5不同,必须经过路由才可以访问到方法,get跟post要分开,就意味着显示页面跟提交不能放在一个方法里面?反正我是没成功的。

  这样才可以的。

3)415,409,500错误

先来说这个简单的,500,是因为我在提交数据的时候,系统默认会有更新update_at和create_at,但是我数据库没有这两个字段,所以报错,要想不让他自主更新,在model上层加一个

public $timestamps = false;

再来说415 unknow method ,好像是因为路由写错了 , get写成了post

还有个409 unknow status , 确实不知道啥原因,百度了一下说是在提交数据的时候加一个

 _token: '{{csrf_token()}}'

如果不用ajax的话在form表单里面直接加一个

{{ csrf_field() }}

好像也是可以的,这个是laravel系统自己的一个表单令牌吧。在这里有讲。

技术最菜,头发最少
原文地址:https://www.cnblogs.com/gushengyan/p/11287847.html