Controller/Parameter syntax? - php

I use CodeIgniter and let's say i build a blog where /posts shows all posts, and /posts/cool_news shows the article called cool_news.
I know the first one would only need a controller called Posts and a function called index(). But how about the second example? how to get the second parameter which can be anything?

Using the default routing:
You create another action in the Posts controller called cool_news()
If you want it to be passed as an argument to the index() action then you need to look at doing some custom routing.
This is untested but should look something like:
$route['posts/(:any)'] = 'posts/index/$1';

Related

Route my form action into a custom function created inside a resource controller

In my web.php,
Route::get('studentmarksheet/addit','StmarksheetController#addit');
Route::resource('studentmarksheet','StmarksheetController');
I created a custom function addit() inside StmarksheetController. I have a form inside a view where I need to pass values to that addit function. For default functions inside my resoource controller, I used to call by
but, while trying to pass form values in addit function, it says route not defined. What exactly should I write? I have tried
{{route('studentmarksheet/addit')}}
{{route('studentmarksheet#addit')}}
and various other combinations.
I am a total beginner and I don't even know if am questioning this correctly. Please share your answers/suggestions/tips and so on, I would love to read them all.
First you need to name a route
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
Then call it
{{route('stmarksheet.addit')}}
Resource route are named for you by Laravel
You didn't set any name for the addit route. So, you need to write your route as like.
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
And then you can get it in the view.
{{ route('studentmarksheet#addit') }}

Laravel: Calling to post method function from the same controller in route

I have three functions in my controller.One of them is GET type and other two is POST type. One POST type is working well but how can i call the second POST method from Route?
i am calling my functions from route like this and they are working well
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
there is another function for Deleting from database which is a post method type. Say the Name of that Function is DeletingRecord(). How can i call this function from Route?
Some considerations:
A controller method is not inherently a POST or GET method. It's the router that decides how to handle a POST or GET request.
If you must use a POST request to delete a record then you must assign it to a different route name. Each route will resolve to exactly one method. For example:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::post('/conference/delete','ViewController#DeletingRecord');
There's no reason why you can't use the DELETE method for this:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::delete('/conference/home','ViewController#DeletingRecord');
You can use delete HTTP verb.
then your code will look like this:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::delete('/conference/home','ViewController#DeletingRecord');
https://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_Web_services
http://www.restapitutorial.com/lessons/httpmethods.html
why you don't use single routing line instead of more routing line as following:
Route::resource('conference', 'ViewController');
for reference please see following link:
https://laravel.com/docs/5.3/controllers#resource-controllers
i hope its help you

PHP codeigniter, how do I make different key in the url so it will call to different controller name instead of the actual controller name?

lets say my url is:
http://www.mysite.com/controller1/
I want to make routing so it wont look for the controller1, it will search for controller1 as the key.
so I will have a key like array for the controllers
<?php
$controllers_mapArr = array(
'controller1'=>'actual_controler_name',
);
?>
it will execute the value of the controller key 'actual_controler_name' but the URL will show controller1
any ideas?
You can easily do this using codeigniter's routing functionality, you have to add rules in application/config/routes.php file, following are the examples for doing
$route['journals'] = "blogs";
if your url is like http://example.com/journals then your URL will call blogs controller
$route['journals/joe'] = "blogs/users/34";
using this rule your if you access your url http://example.com/journals/joe then it will call blogs controller's users method with 34 as a value for function argument.
check the detailed document or routing here : http://ellislab.com/codeigniter/user-guide/general/routing.html
Ok found the answer with a littel help from my friend actually is one of the basics in codeigniter
any way there is the answer:
http://ellislab.com/codeigniter/user-guide/general/routing.html

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

CakePHP: How to route Pagination's sort parameters?

So I'm trying to page items on my index page using the paginator and custom routes. It's all through the index action, but the index action can show items sorted by newest, votes, active or views. Right now, the URL looks like this:
items/index/sort:created/direction:desc
And if you aren't on page one, it looks like this:
items/index/sort:created/direction:desc/page:2
I'd like to use the router to have it look like this:
newest/
I can get that far with this route:
Router::connect(
'/newest/*',
array('controller'=>'items', 'action'=>'index', 'sort'=>'created', 'direction'=>'desc')
);
However, the pager links don't follow the route. As soon as you click next page, you're back to:
items/index/sort:created/direction:desc/page:2
How can I make this follow the router and give me what I want? Keep in mind, it's all from the same controller action, I'm trying to route the sort parameters of pagination basically.
For me your code is working (I've tested your example). Have you done something unusual with the paginator helper?
Here is my Routes:
Router::connect('/newest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'desc'));
Router::connect('/oldest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'asc'));
And here are the urls which I've seen when I sort by age column:
http://localhost/cakephp/1.3.0/newest/page:1
http://localhost/cakephp/1.3.0/newest/page:2
http://localhost/cakephp/1.3.0/newest/page:3
And oldest:
http://localhost/cakephp/1.3.0/oldest/page:1
http://localhost/cakephp/1.3.0/oldest/page:2
http://localhost/cakephp/1.3.0/oldest/page:3
And it's working with all links in the pager (first, prev, 1,2,3 next, last).
You want to include the passed args I think. Something like this,
$this->params = $this->passedArgs();
Have a check here also, http://book.cakephp.org/view/46/Routes-Configuration
Otherwise I would extend the HTML Helper to create my own link method which read in the parameters from the url and created a link accordingly. Then you could manage your own links from your own helper :)
Don't forget that you need to have checks in the index action to deal with this. Personally I would be far more inclined to create an action in the controller for each of these.
function newest(){
}
function votes(){
}
function active(){
}
//etc

Categories