Removing part of a Laravel URL if certain conditions apply - laravel

I have a number of routes on my website that use a custom pagination solution, like /categories/{category}/page/{page?}. I currently have each controller set like the following:
public function index($category, $page = null) {
if(!isset($page) || !is_numeric($page) || $page < 1) {
return redirect()->route('parent', ['category' => $category]);
}
}
This has the desired effect of checking if the provided page number exists, is, indeed, a number, and if it is greater than zero. In the event that any of those conditions are false, it redirects to the parent route, /categories/{category}.
The problem is, for each of these solutions, they all require me to write in the name of the parent route to do the redirect. If I want to just cut off the /page/ and onwards, but keep any get variables and post variables, how would I do that?

If you are trying to redirect back to the previous page with the inputs, use the following code.
return redirect()->back()->withInput();
In the above case, it will work only if you are coming to the url /categories/{category}/page/{page?} from /categories/{category}

Related

wordpress shortcode within get_the_content() and infinite nested loop

First of all, sorry for this long subject, but I didn't find a way to explain the issue otherwise...
I'm facing an issue with shortcode executed within get_the_content() and with the management of infinite nested loop that can occur.
My plugin is used as a shortcode to display posts of a specific list of categories passed as argument.
The shortcode can be used as well on a page, on a post or on a widget.
It can be summarized like this:
Root Post/Page/Widget body with [shortcode category_slug_list="category_slug1"]
=> post 1 of category with slug1 is displayed
post 2 of category with slug1 is displayed
...
...
post n of category with slug1 is displayed
Each post is displayed in the loop with:
$content = get_the_content();
$content = apply_filters('the_content', $content);
If it's used on a post, the main problem here is that the post MUST NOT belong to one of the categories specified by the shortcode to avoid infinite nested loop.
When the post that belong to one of the categories is at first level of depth, the management of this is done with:
if (is_single()) {
if (in_category($category_slug_array)) {
// The function must not proceed with this post !!!
$out = '<div>To proceed inside a post, the post MUST not belong to one of the categories asked by "category_slug_list" option, to avoid infinite nested loop...</div>';
return $out;
}
}
It works because (is_single) is related to the first level of depth and here the first level is a post.
But, for example, when the first level of depth is a page, when one of the posts displayed has its own shortcode there is a problem.
As the content of the post is retrieved with get_the_content(), when the shortcode in it is executed, I can't find a way to retrieve the context, as is_single() returns false because it's related to the root page, and not the current post from which we got the value of get_the_content().
Example:
Root Page body with [shortcode category_slug_list="category_slug2"]
=> post 1 of category with slug2 displayed
post 2 of category with slug2 displayed
post 2 body with [shortcode category_slug_list="category_slug2"]
...
...
post n of category with slug2 displayed
From this example, is there a way to get the "post 2" context, I mean verify that it's a post, and then check the in_category function on it?
Any clue, Idea, Advice will be welcomed...
So, I've finally found a solution to this:
The only way I've found was to be able to pass the post id as attribute to the nested shortcode.
In the shortcode function, in the posts loop, I use str_ireplace in the post content to add attribute if the shortcode is found:
$content = str_ireplace("[shortcode-function-name",'[shortcode-function-name post_id="'.$post->ID.'"',$content);
So, at the beginning of the shortcode function, I retreive the post_id attribute:
extract(shortcode_atts(array(
"post_id" => '',
"other_attribute" => 'default_value',
...
...
"last_attribute" => 'default_value'
), $atts));
And then, in addition to the first check in the case of root post, I can now check with the post ID:
if ($post_id) {
if (in_category($category_slug_array,$post_id)) {
// The function must not proceed with this post !!!
$out = '<div>To proceed inside a post, the post MUST not belong to one of the categories asked by "category_slug_list" option, to avoid infinite nested loop...</div>';
return $out;
}
}
In case it can help someone who try to work with a shortcode function inside a nested loop depending on post specificities...

Session variable on refresh

I have laravel controller like this:
public function postSessionTopic() {
$article_id = Input::get('article_id', 0);
$comment_id = Input::get('comment_id', 0);
\Session::set('page_topic_id', $article_id);
\Session::set('page_comment_id', $comment_id);
\\comment - I have tried \Session::put too, but that doesn't change anything
}
I use it, when user click on a article. I print_r out my session variable in this controller and everything looks fine. But after that I refresh my page, and there I read value from session, and sometimes it load old value or doesn't load anything. I can't understand why, because in controller i can see, that correct value is saved!
In my page, i get that value like this:
\Session::get('page_topic_id', 0)
Probably you do something wrong. You should make sure that in both cases you uses exactly same domain (with or without www).
In this controller when you don't have any input you set to session variables 0. This can also be an issue if you launch this method when you don't have any input.
You could try with adding this basic route:
Route::get('/session', function() {
$page_topic = Session::get('page_topic_id', 1);
$page_comment = Session::get('page_comment_id', 1);
echo $page_topic.' '.$page_comment.'<br />';
$article_id = $page_topic * 2;
$comment_id = $page_comment * 3;
Session::set('page_topic_id', $article_id);
Session::set('page_comment_id', $comment_id);
});
As you see it's working perfectly (but you need to remove session cookie before trying with this path).
You get
1 1
2 3
4 9
8 27
and so on. Everything as expected
Answer was - two ajax at one time. Don't do that, if you store something in session.
The session in Laravel doesn't consider changes permanent unless you generate a response (and that's the result of using symphony as it's base). So make sure your app->run() ends properly and returns a response before refreshing. Your problem is mostly caused by a die() method somewhere along your code or an unexpected exit of PHP instance/worker.
This is probably not your issue but if you are storing your laravel session in the database their is a limit on how large that value can be. The Laravel session migration has a field called "payload" that is a text type. If you exceed the limit on that field the entire session gets killed off. This was happening to me as I was dynamically adding json model data to my session.
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->text('payload');
$table->integer('last_activity');
});
How much UTF-8 text fits in a MySQL "Text" field?

CakePHP Pagination sort() on Related Models

I have two models: Plans and PlanDetails.
Relationship is: PlanDetails hasMany Plans. Plans belongTo PlanDetails.
In the PlanDetails view.ctp, I am pulling in related Plans.
I am trying to sort the Plans by ANY field (I've tried them all), and I cannot get it working. I assume I am overlooking something very simple.
Here is my base code:
PlanDetail >> view.ctp:
...foreach ($planDetail['Plan'] as $plan_edit) :
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}...
<?php echo $this->Paginator->sort('Plan ID', 'Plan.id'); ?>...
...<?php echo $plan_edit['id']; ?>
plan_details_controller.php:
...function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid plan detail', true));
$this->redirect(array('action' => 'index'));
}
$this->PlanDetail->recursive = 2; // To run the editable form deeper.
$this->set('planDetail', $this->PlanDetail->read(null, $id));
$this->set('plan', $this->paginate('Plan'));
}...
I should add, no errors are being thrown and the sort() arrows on the ID field are showing as expected, but the sort order DOES not change when clicked either way.
Sorry, I'm not able to comment on the question itself, but I've noticed that in your action, you set planDetail to be the PlanDetail record you read (with recursive set to 2), and then you set plan to be the result of the paginate call.
Then, in your view template, you're iterating over $planDetail's contained Plan association, like this:
foreach ($planDetail['Plan'] as $plan_edit):
But in order to get the sorting and pagination done, you need to be displaying the results of the paginate call i.e. iterate over the records contained in $plan.
Do a debug($plan) in your view template to see what results you get there and to see if the records' ordering changes when you sort by different fields.
Also, perhaps you're using syntax I'm not aware of, but if you simply call $this->paginate('Plan') in your controller, I don't know that you're going to get only the related Plan records for your particular PlanDetail record. (There's nothing tying the $id passed into your view action with the Plan records.) You might need to add some conditions to the paginate call, like so:
$this->paginate['Plan'] = array('conditions' => array('Plan.plan_detail_id' => $id));
$this->set('plans', $this->paginate('Plan'));
Here is what I did to solve this. Based on some helpful direction from johnp & tokes.
plan_details/view.ctp:
...$i = 0;
foreach ($plan as $plan_edit) : // note $plan here.
}...
In my plan_details_controller.php view action:
$conditions = array("Plan.plan_detail_id" => "$id");
$this->set('plan', $this->paginate('Plan', $conditions)); // note "plan" in the first argument of set (this is required to pass the results back to the view). Also. The $condition is set to return ONLY plans that match the plan_detail_id of id (on the plan_details table).
And in my view, in order to get my results (because I changed the array name), I had to change the way I was getting the values to:
$plan_edit['Plan']['modified'] // note I placed ['Plan'] in front of these calls as the array called for this to get the data...
Well until the next problem! SOLVED.

codeigniter category -> subcategory -> subsubcategory system

ok, so i'm very new to codeigniter and from what i have learned by now i can't figure out how can i create a dynamic category -> subcategory -> subsubcategory system. Can you give me some guidelines please...some references, anything to guide me on what should i learn to accomplish that? thanks
i should get my URL like this www.site.com/category/subcategory/subsubcategory/etc... , you know what i mean.
I have done this for the page manager in PyroCMS but it is no easy task.
Each page has its own slug and parent_id, then to read the correct page it loops through each of the page slugs and joins the child. It knows how many children there are so if there are 5 children it selects the 5th self-joined table.
Here is an example of the code:
public function get_by_path($segments = array())
{
// If the URI has been passed as a string, explode to create an array of segments
if(is_string($segments))
{
$segments = explode('/', $segments);
}
// Work out how many segments there are
$total_segments = count($segments);
// Which is the target alias (the final page in the tree)
$target_alias = 'p'.$total_segments;
// Start Query, Select (*) from Target Alias, from Pages
$this->db->select($target_alias.'.*');
$this->db->from('pages p1');
// Loop thorugh each Slug
$level = 1;
foreach( $segments as $segment )
{
// Current is the current page, child is the next page to join on.
$current_alias = 'p'.$level;
$child_alias = 'p'.($level - 1);
// We dont want to join the first page again
if($level != 1)
{
$this->db->join('pages '.$current_alias, $current_alias.'.parent_id = '.$child_alias.'.id');
}
// Add slug to where clause to keep us on the right tree
$this->db->where($current_alias . '.slug', $segment);
// Increment
++$level;
}
// Can only be one result
$this->db->limit(1);
return $this->db->get()->row();
}
It's a bit nuts but it works perfectly. This can be really slow so PyroCMS also maintains a look-up table which has id and the page URI to match quickly.
You can see the whole model here:
http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php
you could:
create controller category, reroute some URIs to it and use it's internal logic to parse it's arguments to pick whatever article client requested:
About URLs:
http://codeigniter.com/user_guide/general/urls.html
About URI routing:
http://codeigniter.com/user_guide/general/routing.html
I agree with Phil's idea and I was also envisioning that you can create a separate module (if you use modular extensions for example) to handle the categories in a generic way. Then you can reuse that module in any other projects. Basically the new module may be able to handle categories and sub-categories (the hierarchy).

Codeigniter: Passing form data from view to controller

Which is right? notice in the second option, I'm passing the form values using the $_POST variable. Whereas the first option, I call and assign variables for each form field.
I've seen this ...
<validation code> ....
$todo = array(
'name'=>$this->input->post('title'),
'description'=>$this->input->post('description')
);
$this->Todo_model->add($todo);
But I've also seen the following ...
$records['email'] = "trim|required|min_length[4]|xss_clean";
...
...
$this->validation->set_rules($records);
if ($this->validation->run())
{
$this->account_model->saveAccountSettings("sam", $_POST);
$this->session->set_flashdata('message', 'Done!');
redirect('account/settings');
} else {
...
}
I tend to use a mix of your two examples. I'm pretty sure things like trim won't modify the actual post data, so you can only take advantage of it if you go through the validation framework to get the data. I actually never access POST directly anymore using CI.
Plus I'd be worried in your second example about just shoving POST into my model. What happens if someone clever adds "lastname" to the post data sent in and your db column is named the same? Even though you weren't expecting to deal with that data now you've got unvalidated data coming in. That's why I employ part of your first example and manually pull out the items I want to save into an array first.
So I'd recommend a hybrid.
Normally my code looks something like this:
$fields['email'] = "trim|required|valid_email|min_length[4]|xss_clean";
...
...
$this->validation->set_rules($fields);
if ($this->validation->run())
{
$account = new array();
$account['id'] = $accountId; //wherever you get the Id from
$account['email'] = $this->validation->email;
$this->account_model->save($account);
$this->session->set_flashdata('message', 'Done!');
redirect('account/settings');
} else {
...
}
The first option is better easy to read or trace
Pass values using post variables is better option
What the real benefit to use this
$account['email'] = $this->validation->email;
Instead of
$account['email'] = $this->input->post('email');

Resources