CakePHP 编程笔记3

本文详细介绍了CakePHP中用户登录验证流程、取消关联查询的方法、处理访问不完整对象错误、设置及读取URL参数的方式以及如何获取当前URL和域名等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 用户登录验证流程

users controller : beforefilter

-> app controller : beforefilter

-> auth component : startup

-> auth component : login

-> auth component : admin_login

->render view template for login

->press submit, post form data to controller, repeat the above process again.

2. 取消关联查询

You can use recursive or unbind whenever you want to unbind associations from models. This is very useful because there are cases when you want to execute a simple query and you do not want to join other tables. Here comes unbind. Unbind allows us to unbind all but some model associations. On the other hand if you want to unbind all associations you can use recursive like this:

$this-> Model-> recursive = - 1;
3. 访问不完整对象错误
在enable persist Component后,可能会遇到如下的错误提示:
Notice (8): Model:: resetAssociations() [<a
href=' http://php.net/model.resetassociations' >model.resetassociations</
a>]: The script tried to execute a method or access a property of an
incomplete object.
这样的错误可能是1.3的persist component 和 cache 存在bug引起的, 某个类在序列化和cache的时候有点问题, 导致反序列化时找不到类定义。解决方法:把\app\tmp\cache\persistent 和 \app\tmp\cache\model目录清空重新加载。
4.设置和读取url参数

读取参数

http://book.cakephp.org/view/55/The-Parameters-Attribute-params

$this->params['url']

Stores the current URL requested, along with key-value pairs of get variables. For example, if the URL /posts/view/?var1=3&var2=4 was called, $this->params['url'] would contain:

[url] => Array
(
    [url] => posts/view
    [var1] => 3
    [var2] => 4
)
设置参数
http://book.cakephp.org/view/1448/url
URL with GET params and named anchor
Plain Text View
<?php echo $this->Html->url(array(
    "controller" => "posts",
    "action" => "search",
    "?" => array("foo" => "bar"),
    "#" => "first"));
?>

//Output
/posts/search?foo=bar#first
  1. <?php echo $this->Html->url(array(
  2. "controller" => "posts",
  3. "action" => "search",
  4. "?" => array("foo" => "bar"),
  5. "#" => "first"));
  6. ?>
  7. //Output
  8. /posts/search?foo=bar#first

5. how to get current url in view

if you want to take current URL in cakePHP view than check out below code :

echo Router::url($this->here, true);

This will output current URL. If you want relative path and not full URL than you can do like this :

echo $this->here;

By this you can get relative path in the view.

6. how to get domain name

// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i", windows.location, $matches);
$host = $matches[2];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值