Composer安装laravel5.2
composer下载
- 英文网站:https://getcomposer.org/
- 中文镜像:http://www.phpcomposer.com/
composer安装Laravel
修改composer的全局配置文件
composer config -g repo.packagist composer https://packagist.phpcomposer.com
1、直接下载安装(需要翻墙)
创建一个名为 laravel 的 Laravel 项目
composer create-project laravel/laravel --prefer-dist
创建一个名为 blog 的 Laravel 项目
composer create-project laravel/laravel blog --prefer-dist
2. 一键安装包安装
一键安装包下载:http://www.golaravel.com/download/
github下载:https://github.com/laravel/laravel
Laravel初始化配置
1、wamp版本需求(PHP 版本 >= 5.5.9|Wamp2.5)
2、开启rewrite和vhost
C:wampinapacheapache2.4.9confhttpd.conf
3、开启php扩展
php.ini:C:wampinphpphp5.5.12php.ini
extension=php_openssl.dll
extension=php_mbstring.dll
extension=php_pdo_mysql.dll
Laravel5.2目录结构及composer.json文件解析
使用artisan命令创建控制器
1.控制台环境,切换到项目根目录下并输入命令:php artisan make:controller IndexController,这样就创建好一个控制器
2.但是,通常情况下,我们需要对项目进行分组(比方说前后端分组)。如下图所示,在Controllers目录下新建了一个Admin分组,用于存放后台控制器
php artisan make:controller Admin/IndexController
控制器中的index方法
public function index(){
return view('admin/index');
}
Route::get('/', function () {
return view('test'); // 其模板放在resources/views/test.blade.php
});
Route::get('view','AdminViewController@index'); //访问 /view 直接找到Admin下的ViewController下的index方法
Route::get('/admin/{id}','AdminIndexController@index'); // 携带id
// 命名路由
第一种:
Route::get('/test',['as'=>'profile',function(){
return route('profile'); // http://blog.com/test
}]);
第二种:
Route::get('/test', [
'as' => 'profile', 'uses' => 'AdminTestController@index'
]);
第三种:
Route::get('/test','AdminTestController@index')->name('profile');
// 路由分组
Route::group(['prefix'=>'admin','namespace'=>'Admin'],function(){
Route::get('index','IndexController@index');
Route::get('test','TestController@index');
Route::resource('article','ArticleController');
});
F:PHPCodelog>php artisan route:list
+--------+-----------+------------------------------+-----------------+------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------------+-----------------+------------------------------------------------------+--------------+
| | POST | admin/article | article.store | AppHttpControllersAdminArticleController@store | web |
| | GET|HEAD | admin/article | article.index | AppHttpControllersAdminArticleController@index | web |
| | GET|HEAD | admin/article/create | article.create | AppHttpControllersAdminArticleController@create | web |
| | DELETE | admin/article/{article} | article.destroy | AppHttpControllersAdminArticleController@destroy | web |
| | PUT|PATCH | admin/article/{article} | article.update | AppHttpControllersAdminArticleController@update | web |
| | GET|HEAD | admin/article/{article} | article.show | AppHttpControllersAdminArticleController@show | web |
| | GET|HEAD | admin/article/{article}/edit | article.edit | AppHttpControllersAdminArticleController@edit | web |
| | GET|HEAD | admin/index | | AppHttpControllersAdminIndexController@index | web |
| | GET|HEAD | admin/test | | AppHttpControllersAdminTestController@index | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
+--------+-----------+------------------------------+-----------------+------------------------------------------------------+--------------+
//后台专用路由 根据前缀和命名空间 Route::group(['prefix'=>'admin','namespace'=>'Admin','middleware' => ['web','admin.login']],function(){ Route::get('index','IndexController@index'); //资源控制器 先添加这一行 在用 php artisan make:controller ArticleController // 将ArticleController放到Admin目录下 并用php artisan route:list 查看 Route::resource('article','ArticleController'); });
//中间件的用法 创建admin.login中间件 首先在kernel.php中添加admin.login=>'AppHttpMiddlewareAdminLogin.class' //然后用php artisan make:middleware AdminLogin Route::group(['middleware' => ['web']], function () { Route::get('admin/login','AdminIndexController@login'); Route::get('/', function () { session(['key'=>123]); return view('welcome'); }); Route::get('user', function () { echo session('key'); return 'user'; }); });
protected $routeMiddleware = [ 'auth' => AppHttpMiddlewareAuthenticate::class, 'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class, 'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class, 'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class, //自定义中间件Adminlogin 目录在Middleware/AdminLogin.php //route.php 调用 'middleware' => ['web','admin.login']] 'admin.login'=> AppHttpMiddlewareAdminLogin::class, ];
视图传参
//第一种方式传参 $name="ceshi"; $age = '2'; return view('mylarvel')->with(array('name'=>$name,'age'=>$age)); //第二种方式传参 $data = [ 'name'=> 'ceshi', 'age' => '4' ]; return view('mylarvel',$data); //第三种方式传参 $data = [ 'name' => 'ceshi', 'age' => 3 ]; $title = "正在学习larvel"; return view('mylarvel',compact('data','title'));
laravel中引用css文件
{{asset('/static/css/admin.css')}}