search
HomeBackend DevelopmentPHP Tutoriallaravel5安装 数据库配置 模板 路由

安装;

windows安装

需要PHP版本》=5.4.且开启mcrypt加密扩展

都不想安装的可以从https://github.com/overtrue/latest-laravel 这里下载大神整理好的,解压即可使用

首先等安装cpmposer, 提示openssl错误的打开PHP.INI配置文件将extension=php_openssl.dll 前面的注释去掉,大概在第887行。

copmposer安装过程就不说了。

下面开始Laravel 5安装过程,github有下载,这里分享个网盘连接:http://pan.baidu.com/s/1dD50dWH

下载后解压,为了方便你可以解压到网站根目录下。我这里解压到F:/laravel [当然了,这不是我的根目录,需要配置虚拟主机]。

由于众所周知的原因国内安装composer,laravel比较慢,推荐使用镜像安装。

修改laravel目录的composer.json文件

在末尾结束括号前加上:

,     "repositories": [        {"type": "composer", "url": "http://comproxy.cn/repo/packagist"},        {"packagist": false}    ]

若不能用试试http://pkg.phpcomposer.com/

在文件夹内单击鼠标右键选择安装

几分钟后就安装完成啦!

这时会发现目录下多了个vendor文件夹。

laravel的默认首页是server.php,需要配置下,打开httpd.conf配置文件

找到[大概第249行以下]

<IfModule dir_module>    DirectoryIndex index.html index.php index.htm l.php server.php</IfModule>

加上 server.php重启即可。

试试去浏览器打开http://t.com  试看看, 以你自己的安装为准。

到此为止安装成功了,打开慢的原因是使用了谷歌字体, 可以使用国内CDN,找到F:\laravel\resources\views文件夹,打开welcome.blade.php文件,将第五行改为 

360前端公共库:http://libs.useso.com/

接着去试试写个方法,

找到F:\laravel\app\Http文件夹,打开routes.php,加上

/*|--------------------------------------------------------------------------| Application Routes|--------------------------------------------------------------------------|| Here is where you can register all of the routes for an application.| It's a breeze. Simply tell Laravel the URIs it should respond to| and give it the controller to call when that URI is requested.|*/ Route::get('/', 'WelcomeController@index'); Route::get('home', 'HomeController@index'); Route::controllers([    'auth' => 'Auth\AuthController',    'password' => 'Auth\PasswordController',]);Route::get('hi', function(){    return "hi";});

打开浏览器输入http://t.com/public/hi

试看看,这就是路由了。


如需直接访问到public项目文件夹可以配置vhost

<VirtualHost *:80>    DocumentRoot "F:\laravel\public"    ServerName t.com    ServerAlias t1.zy62.com  <Directory "F:\laravel>      Options FollowSymLinks ExecCGI      AllowOverride All      Order allow,deny      Allow from all      Require all granted  </Directory></VirtualHost>



2.linux安装

laravel必须PHP>5.4支持mcrypt。

从https://github.com/overtrue/latest-laravel 这里下载大神整理好的,解压即可使用

nginx伪静态:[我这里用的是LNMP]

server    {        listen 80;        #listen [::]:80;        server_name laravel.zy62.com;        index index.html index.htm index.php default.html default.htm default.php;        root  /home/wwwroot/laravel.zy62.com/public;         include other.conf;        #error_page   404   /404.html;        location ~ .*\.(php|php5)?$        {            try_files $uri =404;            fastcgi_pass  unix:/tmp/php-cgi.sock;            fastcgi_index index.php;            include fcgi.conf;        }         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$            {                expires      30d;            }         location ~ .*\.(js|css)?$            {                expires      12h;            }if (!-d $request_filename)            {                rewrite ^/(.+)/$ /$1 permanent;            }             # removes trailing "index" from all controllers            if ($request_uri ~* index/?$)            {                rewrite ^/(.*)/index/?$ /$1 permanent;            }             # unless the request is for a valid file (image, js, css, etc.), send to bootstrap            if (!-e $request_filename)            {                rewrite ^/(.*)$ /index.php?/$1 last;                break;            }        access_log  /home/wwwlogs/laravel.zy62.com.log  access;    }

include fcgi.conf;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;fastcgi_param  QUERY_STRING       $query_string;fastcgi_param  REQUEST_METHOD     $request_method;fastcgi_param  CONTENT_TYPE       $content_type;fastcgi_param  CONTENT_LENGTH     $content_length; fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;fastcgi_param  REQUEST_URI        $request_uri;fastcgi_param  DOCUMENT_URI       $document_uri;fastcgi_param  DOCUMENT_ROOT      $document_root;fastcgi_param  SERVER_PROTOCOL    $server_protocol; fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version; fastcgi_param  REMOTE_ADDR        $remote_addr;fastcgi_param  REMOTE_PORT        $remote_port;fastcgi_param  SERVER_ADDR        $server_addr;fastcgi_param  SERVER_PORT        $server_port;fastcgi_param  SERVER_NAME        $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirectfastcgi_param  REDIRECT_STATUS    200;

ERROR: An another FPM instance seems to already listen on /tmp/php-cgi-714ms.com.sock


解决办法:

/root/lnmp reload


数据库配置:\config\database.php

'mysql' => [            'driver'    => 'mysql',            'host'      => 'localhost',            //'host'      => env('DB_HOST', 'localhost'),            'database'  => 'test',            //'database'  => env('DB_DATABASE', 'forge'),            'username'  => 'root',            //'username'  => env('DB_USERNAME', 'forge'),            'password'  => 'qaz123',            //'password'  => env('DB_PASSWORD', ''),            'charset'   => 'utf8',            'collation' => 'utf8_unicode_ci',            'prefix'    => '',            'strict'    => false,        ],

视图,传值:

路由:

Route::get('/hi',function(){    //echo 123;    return View::make('hi')->with('name','laravel');//way1});

在laravel\resources\views下新建hi.blade.php文件

<?php?> <html> <head>  <meta charset="UTF-8" />  <title>test</title>  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script><link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> </head> <body>  <div>   <h1 id="laravel-nbsp-view">laravel view</h1>  <p><?php echo $name; ?></p></div>   </body></html>

视图传值方式:

return View::make('hi')->with('name','laravel');//way1

2.魔术方法

return View::make('hi')->withName('laravel');
$data['name']='laravel';return View::make('hi',$data);



路由传值限制:

那么现在我的博客有一堆文章,我想通过地址 http://yourdomain/article/1 访问 id 为 1 的文章,通过 http://yourdomain/article/2 访问 id 为 2 的文章怎么办?

Route::get('article/{id}', function($id) {    return 'Article:' . $id;});

但是,假如有这样一种情况,有很多地方都需要 id 作为参数,id 大都是这种数字,我们肯定不希望每一个路由规则都要写一次 where 方法来设置 id 的匹配规则。这时候可以通过这样一个方法来实现:

Route::pattern('id', '[0-9]+');


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools