How to get pretty URL in Codeigniter? - codeigniter

I have a product controller (Codeigniter) where I load 40 categories in my index function. When I click on a category, I want to load all item of that particular category. To do that, I can easily write a function to load all that items.
Function load_item($categoryId’)
{
// in short
…… where categoryId = ‘$categoryId’
// then load it to view
}
Then I have URL like /product/load_item. But I want URL like /product/laptop or /product/desktop (/product/category_name). So, it's not possible to write 40s function for every category and also its not optimal solution. I don’t want to change anything in index function. Have you any idea please???

You have to setup the routes for the url's so under your config folder go to routes and then create a route like so
$route['product/(:any)'] = 'catalog/product_lookup';
You can find all relevant information in the Codeiginter User Guide

Method 01
In routes.php
$route['category/(:any)'] = 'category/load_item';
Output - www.example.com/category/(value/number-of-category)
Method 02
In View URL passing method(Means <a>)
Click me
so in controller
Function item($catgoryName, $categoryId)
{
// in short
where categoryId = '$categoryId';
// then load it to view
}
Output - www.example.com/category/item/laptops/1

Related

How do I pass a value in my Route to the Controller to be used in the View in Laravel?

I have 2 entities called Match and Roster.
My Match routes are like this
http://localhost:8888/app/public/matches (index)
http://localhost:8888/app/public/matches/14 (show)
In order to view/create the teams for each specific match I added the routes for the match roster like this:
Route::get('/matches/'.'{id}'.'/roster/', [App\Http\Controllers\RosterController::class, 'index']);
Now I need that {id} i have in my URL to pass it to the Controller here:
public function index()
{
return view('roster.index');
}
I need that for a couple of things. First I need to do a search on the Roster table filtering by a column with that value, so I can display only the players that belong to that match.
Second, I need to pass it on to the view so I can use it on my store and update forms. I want to add or remove players from the roster from that same index view.
How can I do that?
#1 You can get the route parameter defined on ur routes via request()->route('parameter_name').
public function index()
{
// get {id} from the route (/matches/{id}/roster)
$id = request()->route('id');
}
#2 You can pass the data object via using return view(file_name, object)
public function index()
{
// get {id} from the route (/matches/{id}/roster)
$id = request()->route('id');
// query what u want to show
// dunno ur models specific things, so just simple example.
$rosters = Roster::where('match_id', '=', $id);
// return view & data
return view('roster.index', $rosters);
}
#3 It can be done not only index but also others (create, store, edit, update)
In addition, STRONGLY RECOMMEND learn Official Tutorial with simple example first.
Like a Blog, Board, etc..
You need to know essentials to build Laravel App.
Most of the time, I prefer named routes.
Route::get('{bundle}/edit', [BundleController::class, 'edit'])->name('bundle.edit');
In controller
public function edit(Bundle $bundle): Response
{
// do your magic here
}
You can call the route by,
route('bundle.edit', $bundle);

Passing data from blade to blade in Laravel

I have a page where you can create your workout plan. Second page contains "pre-saved" workouts and I want them to load by passing parameters from second page to first. If you directly access first page, you create your workout plan from scratch.
// first page = https://prnt.sc/y4q77z
// second page = https://prnt.sc/y4qfem ; where you can check which one you want to pass to first page
// final step looks like this: https://prnt.sc/y4qh2q - but my URL looks like this:
www.example.com/training/plan?sabloni%5B%5D=84&sabloni%5B%5D=85&sabloni%5B%5D=86
this 84,85,86 are IDS
Can I pass params without changing URL ? Like having only /training/plan without anything after ?
public function plan(Request $request){
$workout = false;
if($request->workout){
$workout = $request->workout;
$workout = SablonTrening::find($sabloni); // $workout = array [1,3,4,5,6]
}
return view('trener.dodaj_trening', compact('workout'));
}
If you are getting to the /training/plan page with GET request, you could simply change it to POST. That way the parameters would be hidden in the URL but would be present in the request body. You would need a new post route:
Route::post('/training/plan', 'YourController#plan')->name('training.plan');
And then, in the form where you are selecting these plans, change the method on submit:
<form action="{{route('training.plan')}}">
//Your inputs
</form>
Your method should still work if your inputs stay the same.
Note: Not sure you would still keep the functionalities that you need, since I can't see all the logic you have.
If you have any questions, let me know.
To pass data from on blade to another blade.
At the end of first post before redirect()-route('myroute') add $request->session()->put('data', $mydata);
At the begining of the route 'myroute', just get back your data with $data = $request->old('data');

Passing a count from a relationship on a simple page in Laravel

I was curious if it was possible to pass along a count of a relationship from a controller and put it on a simple page like a home page (which isn't specifically related to any specific model or controller). So say a user hasMany shipments, how can I pass along the count to the page?
I know how to pass along variables to model specific pages (such as show, edit, index and such pages), but not a general pages such as a home page or about page.
You should at least have a PageController and have a method like homepage like this.
class PageController extends Controller {
protected $data = array();
public function homepage() {
$this->data['count'] = 10;
return view('homepage', $this->data);
}
}
$data is an array of all the data you would like to pass to your views. As for this example, you can access them in your blade template like this:
{{ $count }}
You can enable cache in laravel. It is very simple and usefull. You can store your variable in this cache.
For more information, you could read documentation

Dynamic Controllers in CodeIgniter

I am in the process of creating a new website which loads all master and child categories from the database. I have tested the navigation as well, i.e., if I click any master category, it perfectly loads all the respective child categories without any issue. However, at present, I am doing this by passing query string in the URL. For instance
http://localhost/MyController?id=32145
Let's assume that the id, 32145, represents a master category namely 'About us'. My question is how can I change the above URL to something like:
http://localhost/Aboutus
and if there is any child category under About us than it should display as:
http://localhost/Aboutus/Mission
Please help me out as I am really stuck with this problem.
by default CodeIgniter uses a segment-based approach, you can do URL routing in way like your second part of the question - "and if there is any child category under About us"
$route['product/(:any)'] = "catalog/product_lookup";
more here: https://ellislab.com/codeigniter/user-guide/general/routing.html
but if you want to rewrite complete URL than you should probably check .htaccess rewriting
It is not easy, do once for migration.
In database you can store the New controller/url for products (if it is not have yet)
Create new Controllers
Route controller which redirect the old Url to the New Url
controllers
Route old urls to Route controller
Route controller something like this:
public function old_url($aProdId) {
if (is_null($aProdId)) {
// error cannot be null
}
$NewUrl = $this->new_url_model->getNewUrl($aProdId);
if (!$NewUrl) {
// error new url not exist
return;
}
redirect(base_url($NewUrl), 'refresh');
}

Codeigniter: Routing and URI Segments

I'm having an issue with routing in codeigniter.
Lets say I have a controller named Pages, with a method named product that does the following:
public function product() {
$this->load->model('pages_model');
$productid = $this->uri->segment(3);
$data['product'] = $this->pages_model->getProduct($productid);
// ...load view, etc.
}
To access a particular product, my url will be www.example.com/pages/product/ID.
I want to setup a custom route so I can access the product by going to www.example.com/name-of-product.
However, putting
$route['name-of-product'] = 'pages/product/ID';
does not work. It will load the product view, but the product data will not be loaded. If I say
$route['name-of-product/:any/ID'] = 'pages/product/ID';
it works as it should, but I would rather not have the two additional segments at the end of the url.
You don't need 2 additional segments. One should be sufficient.
$route['PRODUCT_NAME/PRODUCT_ID'] = 'pages/product/PRODUCT_ID';
However, if I were you I would make the URL to have the first segment to be the id of the product instead.
$route['PRODUCT_ID/PRODUCT_NAME'] = 'pages/product/PRODUCT_ID';
That way, if I only know the product id, I wouldn't have to type example.com//123 which might cause some problem. If I'm not mistaken, if you do that, CI will try to load a controller named 123.

Resources