Welcome Guest, Not a member yet? Register   Sign In
Dealing with route (universal search)
#1

(This post was last modified: 08-15-2023, 07:29 AM by tassman.)

Do you have way to deal witch search on your site with segemnts.
Example:
Exist database with three fields: priority, date, user, info
Later I want to  enter URL "www.mysite.com/20230815/jack" and I now that will start seach in my databes date 2023-08-15 and jack  and throw in my table.
As you see priority is not taken into consideration at this time of seach.
Thx for your suggestions.

Maybe something like this:
www.mysite.com/date_20230815/user_jack

And then I can
$uri->getSegments();
and process witch example regex to catch colums to search.
Is this OK?
Reply
#2

You can use "www.mysite.com/20230815/jack" with the defined routes.
See https://www.codeigniter.com/user_guide/i...ting-rules
Reply
#3

Sorry. I have read this but I dont had idea how to check for omitted data in segment.
Something like:
In my project www.mysite.com/20230810/empty/jack
And for real will be www.mysite.com/20230810/jack

In this case as I now 3th segment will be treated as 2nd segment, but I dont want that.
Reply
#4

I think I understand what you're talking about. In this case, I would recommend having default values in the URL. For example, https://www.mysite.com/_low_/20230815/jack/_info_ where __low_ is the default priority, _info_ is the information if it is not set.


Or you can create several rules for each case when some parameter is missing. Replace it with the default string.
You need to check this solution, I can't say right now.


PHP Code:
<?php
// Var filled from URL
// Calls $model>list($prior = '__low_', $date, $user, $info = '__info_')
$routes->get('(:segment)/(:segment)''Model::list/_low_/$2/$3/_info_);

// Calls $model>list($prior, $date, $user, $info)
$routes->get('
(:segment)/(:segment)/(:segment)/(:segment)', 'Model::list/$1/$2/$3/$4'); 
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(This post was last modified: 08-17-2023, 05:31 PM by kenjis.)

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index($date$user): string
    
{
        // in real site, you should validate $date and $user values.

        return '$date: ' esc($date) . ', $user: ' esc($user);
    }


PHP Code:
$routes->get('(:num)/(:segment)''Home::index/$1/$2'); 

Navigate to http://localhost:8080/20230810/jack
Reply
#6

Ozornik: https://forum.codeigniter.com/showthread...#pid411843
Good idea. Thanks to you I see another solution. By default segment may be "any" or other word couse searching all result in database on specific column.
Example
www.mysite.com/any/low/any/jack

kenjis: https://forum.codeigniter.com/showthread...#pid411846
I dont understand what you whant to explain with this example.
Reply
#7

I showed how to use the URL that you want.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB