• php laravel框架学习笔记 (一) 基本工作原理


    原博客链接:http://www.cnblogs.com/bitch1319453/

    安装laraver完成后,在cmd中进入laravel目录,使用命令php artisan serve开启8000端口服务器

    然后简单介绍一下laraver的工作流程。这个工作流程包含了页面编写以及传递参数,可以进行一些基本工作了

    开始页面

    与其他框架不同的是,框架中有一个route选项。打开app/http/request/route.php,你将看到

    <?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('/', function () {
    return view('welcome');
    });

    view('welcome')就是localhost:8000的页面。你可以在route里直接return一个字符串,这样将返回一个字符串页面。

    查看view,打开resource/view,发现welcome.blade.php。这就是return返回的页面,blade似乎是指运用的模板

    最简单粗暴的,我们可以直接在route里添加匿名函数(匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。)

    使用匿名函数创建新页面处理方法

    如先在view里创建一个site文件夹,文件夹下创建一个about.blade.php,我们要显示它可以这样写

    Route::get('/about', function () {
        return view('site/about');
    });
    

     打开浏览器输入localhost:8000/about可以看见该页面

     使用controller创建新页面处理方法

    因为对每个视图都使用匿名函数是不好的做法,我们可以使用controller来避免匿名函数过多的创建

    在你的laravel是composer创建的情况下,你可以用:php artisan make:controller 名字        这样的命令创建一个controller。打开app/http/controller, 你会发现你刚刚用命令行创建的东西。现在我将给出一个使用它的例子。

    在route.php中添加,其中SiteController是你创建时的名字,@代表调用sitecontroller中的方法

    Route::get('/about','SiteController@index');

    在SiteController中添加函数index

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    
    use AppHttpRequests;
    
    class SiteController extends Controller
    {
        //
        public function index(){
            return view('site/about')->with(
                ['sname'=>'fuck','bitchname'=>'asshole']
            );
        }
    }
    

     嘛,依然是返回一个view视图,这里又用with传递了两个参数sname和bitchname

    然后是site/about.blade.php,一个简单的页面

    <!DOCTYPE html>
    <html>
    <head>
        <title>about </title>
    </head>
    <body>
    <h1> asshole {{$sname}} {{$bitchname}}</h1>
    </body>
    </html>
    

     输入localhost:8000/about可以显示asshole fuck asshole,证明controller使用成功了。顺便提一下blade还有很多妙用,@extend('name')可以继承name中的内容,{{}}也是blade提供的转义写法,除此之外还有{{!! var !!}}不转义的写法。

  • 相关阅读:
    [苹果maccms] MACCMS苹果cms宝塔定时任务添加教程说明
    [苹果cmsV10]新版本演员库分类报无权限问题和解决方法!
    CentOS 6.8安装Python2.7.13
    [HOWTO] Install Sphinx for A Script Pro
    A Script Pro nginx URL重写规则无法播放MP4解决方法
    随机跳转
    UI库
    vuex
    vue 数据请求
    vue守卫、储存与路由模式
  • 原文地址:https://www.cnblogs.com/bitch1319453/p/6798911.html
Copyright © 2020-2023  润新知