Laravel Route::get() function and parameter confusing - laravel-4

Hello everyone I just installed laravel4 and spend two days trying to make the first step. Now I made it but I'm confused about the Route::get() function and his paremeters.
I installe laravel directly in
/opt/lampp/htdocs/laravel
then follow tutorial to create file
userform.php
into app/views, then add following codes into routes.php
Route::get('userform', function()
{
return View::make('userform');
});
. Then I go to
/localhost/laravel/public
to see welcome page, and
/localhost/laravel/public/userform
to see the form defined in the view/userform.php.
Q1: According to chrome dev tools, i see in the html page, the form action is
http://localhost/laravel/public/userform
but there is nothing under public but
index.php, favicon.ico packages robots.txt
Q2: for
Route::get('userform', function()
{
return View::make('userform');
});
what is the first "userform" represent?? according the official tutorial, it's supposed to be url, but what is the former part?
for this line
return View::make('userform')
I guess "userform" referes to the file /app/views/userform.php, right?

The .htaccess file in the public directory is responsible for funnelling all incoming requests through the index.php file. This allows Laravel to grab the URI and match it to the route you defined and eventually return to you the view you made.
So you request localhost/laravel/public/userform, the request is funnelled through index.php and Laravel is booted. Laravel picks off the userform part of the URI and matches it against your defined routes. It finds the route you defined and fires it and returns the response.
You're spot on with what you were thinking with your second question as well. When you call View::make the first argument is the name of the view you want to "make". If you named your view app/views/forms/user.php then you would return it like so in your route:
return View::make('forms.user');
Or you could use a slash:
return View::make('forms/user');

Related

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

codeigniter routing general understanding

I tried to understand how the routing in CodeIgniter work.
I want to use normal access to sides which are not a kind of user area or something special - only normals links in the main root of the website.
So I try this.
I've set in the routes.php
$route['/'] = "index";
I've created controller names Frontend.php and a model named Frontend_modell.php
The Controller (nothing to do)
public function index()
{
redirect(site_url('index'));
}
The Modell
public function __construct()
{
parent::__construct();
}
I've added an index.php inside the view-folder Frontend and I add the index.php (for test) in the main folder from appilation and in the view folder.
If I try to access www.domain.com I see the URL will change to www.domain.com/index, but no side will come up. "the page you requested was not found."
What I do wrong ? I hope somebody can explain to me how it works correctly and why.
First of all routes uses controller_name/method_name and here you tried to use method_name directly.
Secondly, most of cases you don't need to play with routes unless you need a special handler or rerouting, so mostly keep it to default.
Btw the url rerouted to www.domain.com/index cause it looked for a controller named index as you specified and there in no controller found with that name it should be frontend if that's your controller.
The reason for the problem was from another point.
The Controller i've create had some bugs, after checking the function of the CR Controller, the routing works fine :-))

Same route on first link it works, but on second not. Laravel 5.6

Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)
I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.

laravel api route is not redirecting?

I am new to laravel,I had wrote api route code to register controller:
Route::post('test','Api\Auth\RegisterController#index');
In Register controller i had written simple code
public function index(Request $request)
{
return 'hello';
}
I am getting the output in postman like:
Sorry, the page you are looking for could not be found.
not hello.
Here the images:
1 3
Routes defined in the routes/api.php file are nested within a route
group by the RouteServiceProvider. Within this group, the /api URI
prefix is automatically applied so you do not need to manually apply
it to every route in the file.
You are trying to make a request to a route which does not exist.
In Postman
Change:
http://localhost:8080/App/api/test
To:
http://localhost:8080/api/test
You're telling laravel to route assoaciate '/api/test', not '/App/api/test', which is the adress you're trying to reach.
Also, if you plan to reach that address straight from the location bar of your browser, you should register the 'GET' method as well.
As you are returning something in function you need to use route as get() instead of post().
Route::get('test','Api\Auth\RegisterController#index');

Codeigniter Routes Conflict

I am using codeigniter to create a restapi and I am having some problems with routes.
Here is the equation:
I have to navigate /users/ to index function,
I have to navigate /users/{MongoId} to /users/show/{MongoId},
I have to navigate /users/function to /users/function.
And here, my routes:
$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";
When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.
When I write it back (or both), my index function works successfully, but my login doesn't.
Because it tries to send function name to show function as parameter.
Can you please help me to fix that?
Reverse the order of routes, CodeIgniters routes are prioritized.
You seek this structure:
$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";
also use (:any) instead of (.*) they are the same.
CodeIgniter Routing
Here, the working routes.
$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$1";

Resources